thedersen / backbone.validation

A validation plugin for Backbone.js that validates both your model as well as form input
thedersen.com/projects/backbone-validation
MIT License
1.32k stars 300 forks source link

Overriding default message for pattern "email" #279

Open dangelion opened 9 years ago

dangelion commented 9 years ago

Here is explained how to override messages: https://github.com/thedersen/backbone.validation#overriding-the-default-error-messages

_.extend Backbone.Validation.messages, // coffescript
  required: '{0} is required'
  rangeLength: '{0} must be between {1} and {2} characters'

how can specify the message for specific patterns (like "email")?

EDIT: found solution for pattern "email" (you could add it to docs)

_.extend Backbone.Validation.messages,
  required: '{0} is required'
  rangeLength: '{0} must be between {1} and {2} characters'
  email: 'text here'

but what about a custom validator (myPattern)?

_.extend Backbone.Validation.messages,
  required: '{0} is required'
  rangeLength: '{0} must be between {1} and {2} characters'
  myPattern: 'my text here' // <=== This doesn't work

I want to override that cause I want to handle all messages in the same place.

chiefGui commented 9 years ago

If you want to customize the messages of your own pattern, you should do it in the pattern itself, like:

_.extend(Backbone.Validation.messages, {
  myPattern: 'This is an error message'
});

Keep this in mind:

The validator should return an error message when the value is invalid, and nothing (undefined) if the value is valid.

dangelion commented 9 years ago

Hi @chiefGui there is no way to override message for my patterns outside them? I'd like to manage all the messages in the same place, among the default ones

chiefGui commented 9 years ago

Do you want this, right?

_.extend(Backbone.Validation.messages, {
  required: '{0} is required',
  myPattern: 'This is an error message'
});

So, you already did this. What's going on? What's the error?

You said:

myPattern: 'my text here' // <=== This doesn't work

But, what's happening?

dangelion commented 9 years ago

It returns the message specified inside the custom validator instead of the one specified in the _.extend