lykmapipo / sails-hook-validation

Custom validation error messages for sails model with i18n support
104 stars 29 forks source link

It's not working for me #20

Closed slavafomin closed 8 years ago

slavafomin commented 9 years ago

Hello!

I'm trying to use this hook in order to translate my application, however, it's not working.

$ npm list sails
mysite@0.0.0 /home/sfomin/mysite
└── sails@0.11.1
// api/models/User.js
var _ = require('lodash');
var _super = require('sails-auth/api/models/User');
_.merge(exports, _super);
_.merge(exports, {
});
// api/controllers/AuthController.js

var _ = require('lodash');
var _super = require('sails-auth/api/controllers/AuthController');
var Promise = require('bluebird');
var redirectUrl = '/';

_.merge(exports, _super);
_.merge(exports, {

  // ...

  signUp: function signUpAction (req, res) {

    if (req.user) {
      return res.redirect(redirectUrl);
    }

    checkPost()
      .then(createUser)
      .then(redirect)
      .catch(showForm)
    ;

    function checkPost () {
      return ('POST' == req.method ? Promise.resolve() : Promise.reject());
    }

    function createUser () {
      return User
        .register(req.body)
      ;
    }

    function redirect () {
      res.redirect(redirectUrl);
      return Promise.resolve();
    }

    function showForm (err) {
      console.log(JSON.stringify(err));
      var context = {
        form: req.body,
        errors: (err && err.invalidAttributes ? err.invalidAttributes : [])
      };
      res.view('auth/sign-up', context);
    }

  }
});

As you can see I'm also using sails-auth in order to implement authentication.

config/locales/ru.json:

{
  "user.email.unique": "Указанный E-Mail уже занят"
}

I'm getting the standard error message instead of localized one.

// config/i18n.js
module.exports.i18n = {
  locales: ['en', 'ru'],
  defaultLocale: 'ru',
  updateFiles: true
};

Please advise, thanks!

lykmapipo commented 9 years ago

hi @slavafomin

Custom validation messages are available in error.Errors. Try to use that and see if its working.

Hope it helps.

Shkryob commented 9 years ago

It doesn't work for me too. I have changed error.ValidationError to error.invalidAttributes and that helped. I can't understand this behavior because there is a getter for ValidationError https://github.com/balderdashy/waterline/blob/master/lib/waterline/error/WLValidationError.js#L132

lykmapipo commented 9 years ago

@Shkryob & @slavafomin

sails-hook-validation works by adding custom errors message in error.Errors. It also requires you to define i18n files or custom validation messages.

Try to check Sample model and i18n files.

Hope it helps.

Shkryob commented 9 years ago

@lykmapipo ok probably I was not clear. I have changed ValidationError to invalidAttributes in plugin's code, not in my code. Please see https://github.com/lykmapipo/sails-hook-validation/pull/22 I believe this changes are relevant because ValidationError is deprecated in Sails anyway (https://github.com/balderdashy/waterline/blob/master/lib/waterline/error/WLValidationError.js#L132)

lykmapipo commented 9 years ago

@Shkryob

Which sails version you using?

Shkryob commented 9 years ago

@lykmapipo I am using Sails 0.11.0

Clan-Utility commented 8 years ago

Not working for me either.

TypeError: undefined is not a function at grabLocale (...node_modules/sails-hook-validation/index.js:73:63) at routeTargetFnWrapper (...node_modules/sails/lib/router/bind.js:179:5) at callbacks (...node_modules/sails/node_modules/express/lib/router/index.js:164:37)

Does the hook require to set validationMessages for all the models?

lykmapipo commented 8 years ago

@Clan-Utility

Based on its purpose. Its YES

lykmapipo commented 8 years ago

@Shkryob

:+1:

@Clan-Utility @slavafomin checkout v0.4.3