lykmapipo / sails-hook-validation

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

date type validation customization doen't work #5

Closed ctimoteo closed 9 years ago

ctimoteo commented 9 years ago

Hello,

i think that i cannot customize validation messages of attributes with type "date" with your module (sails v0.11 updated and latest module code too),

in my model User i have one attribute set as:

birthday: { type: 'date', required: true, unique: false },

in validationMessages object i added:

birthday: { required: 'Your birthday is required', birthday: 'Birthday is not a valid date' }

when i called the User.create function (in Controller), with a invalid date 'aaa' in attribute birthday,

User.create(data, function(error, user) { (...) });

it returns validation errors as expected, but error.Error is empty (empty object {})

if i retrieve invalidAttributes property from errors, i get the original validation error:

{ birthday: [ { rule: 'date', message: 'undefined should be a date (instead of "aaa", which is a string)' } ] }

am i doing something wrong or date type validation is not customized correctly with your module?

thanks in advance.

lykmapipo commented 9 years ago

@ctimoteo all custom error messages are placed on error.Errors.

Check that if it does not work. Let me know

lykmapipo commented 9 years ago

@ctimoteo

In sails validation are series of rules on attribute which sails-hook-validation tap in to them and pick any validation error found and match it with your custom validation messages.

So your validation messages must contain valid sails validation rules.

To setup your birthday rules(date rule) check

And you can access custom messages as shown on the spec:

Hope it help.

ctimoteo commented 9 years ago

Yes, helped a lot! My mistake was to use the field name instead of rule name as you mentioned. i already added minLength and other rules, everything works now.

Thank you, one more time.

I will close this issue.