Skip to content

Commit

Permalink
Added ability to set error message per pattern. Closes #174
Browse files Browse the repository at this point in the history
  • Loading branch information
thedersen committed Oct 17, 2013
1 parent bc3f5ff commit a23d796
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 8 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -739,12 +739,18 @@ The validator should return an error message when the value is invalid, and noth
If you have custom patterns that are used several places in your code, you can extend the patterns with your own. And if you don't like the default implementation of one of the built-ins, you can override it.
Remember to also provide a default error message for it.
```js
_.extend(Backbone.Validation.patterns, {
myPattern: /my-pattern/,
email: /my-much-better-email-regex/
});

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

var Model = Backbone.Model.extend({
validation: {
name: {
Expand Down Expand Up @@ -874,6 +880,7 @@ Basic behaviour:
#### Master
* Fixed undefined format function when calling one of the built in validators form within a method validator. Fixes #98
* BREAKING: Added ability to set error message per pattern. This means that if you have custom patterns, or have change the default one, you need to [add/change a default message](http://thedersen.com/projects/backbone-validation/#extending-backbone-validation/adding-custom-patterns) for it.
#### v0.8.2 [commits](https://github.com/thedersen/backbone.validation/compare/v0.8.1...v0.8.2)
Expand Down
2 changes: 1 addition & 1 deletion dist/backbone-validation-amd-min.js

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions dist/backbone-validation-amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,11 @@
rangeLength: '{0} must be between {1} and {2} characters',
oneOf: '{0} must be one of: {1}',
equalTo: '{0} must be the same as {1}',
pattern: '{0} must be a valid {1}'
digits: '{0} must only contain digits',
number: '{0} must be a number',
email: '{0} must be a valid email',
url: '{0} must be a valid url',
inlinePattern: '{0} is invalid'
};

// Label formatters
Expand Down Expand Up @@ -621,7 +625,7 @@
// Can be a regular expression or the name of one of the built in patterns
pattern: function(value, attr, pattern, model) {
if (!hasValue(value) || !value.toString().match(defaultPatterns[pattern] || pattern)) {
return this.format(defaultMessages.pattern, this.formatLabel(attr, model), pattern);
return this.format(defaultMessages[pattern] || defaultMessages.inlinePattern, this.formatLabel(attr, model), pattern);
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion dist/backbone-validation-min.js

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions dist/backbone-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,11 @@ Backbone.Validation = (function(_){
rangeLength: '{0} must be between {1} and {2} characters',
oneOf: '{0} must be one of: {1}',
equalTo: '{0} must be the same as {1}',
pattern: '{0} must be a valid {1}'
digits: '{0} must only contain digits',
number: '{0} must be a number',
email: '{0} must be a valid email',
url: '{0} must be a valid url',
inlinePattern: '{0} is invalid'
};

// Label formatters
Expand Down Expand Up @@ -614,7 +618,7 @@ Backbone.Validation = (function(_){
// Can be a regular expression or the name of one of the built in patterns
pattern: function(value, attr, pattern, model) {
if (!hasValue(value) || !value.toString().match(defaultPatterns[pattern] || pattern)) {
return this.format(defaultMessages.pattern, this.formatLabel(attr, model), pattern);
return this.format(defaultMessages[pattern] || defaultMessages.inlinePattern, this.formatLabel(attr, model), pattern);
}
}
};
Expand Down
8 changes: 6 additions & 2 deletions src/backbone-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,11 @@ Backbone.Validation = (function(_){
rangeLength: '{0} must be between {1} and {2} characters',
oneOf: '{0} must be one of: {1}',
equalTo: '{0} must be the same as {1}',
pattern: '{0} must be a valid {1}'
digits: '{0} must only contain digits',
number: '{0} must be a number',
email: '{0} must be a valid email',
url: '{0} must be a valid url',
inlinePattern: '{0} is invalid'
};

// Label formatters
Expand Down Expand Up @@ -607,7 +611,7 @@ Backbone.Validation = (function(_){
// Can be a regular expression or the name of one of the built in patterns
pattern: function(value, attr, pattern, model) {
if (!hasValue(value) || !value.toString().match(defaultPatterns[pattern] || pattern)) {
return this.format(defaultMessages.pattern, this.formatLabel(attr, model), pattern);
return this.format(defaultMessages[pattern] || defaultMessages.inlinePattern, this.formatLabel(attr, model), pattern);
}
}
};
Expand Down
8 changes: 8 additions & 0 deletions tests/validators/pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ buster.testCase("pattern validator", {
this.model.set({email:''}, {validate: true});
},

"has default error message for inline pattern": function(done) {
this.model.bind('validated:invalid', function(model, error){
assert.equals({name: 'Name is invalid'}, error);
done();
});
this.model.set({name:''}, {validate: true});
},

"value not matching pattern is invalid": function() {
refute(this.model.set({
name: 'aaa'
Expand Down

0 comments on commit a23d796

Please sign in to comment.