Skip to content

Commit a23d796

Browse files
committed
Added ability to set error message per pattern. Closes #174
1 parent bc3f5ff commit a23d796

7 files changed

+35
-8
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,12 +739,18 @@ The validator should return an error message when the value is invalid, and noth
739739
740740
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.
741741
742+
Remember to also provide a default error message for it.
743+
742744
```js
743745
_.extend(Backbone.Validation.patterns, {
744746
myPattern: /my-pattern/,
745747
email: /my-much-better-email-regex/
746748
});
747749

750+
_.extend(Backbone.Validation.messages, {
751+
myPattern: 'This is an error message'
752+
});
753+
748754
var Model = Backbone.Model.extend({
749755
validation: {
750756
name: {
@@ -874,6 +880,7 @@ Basic behaviour:
874880
#### Master
875881
876882
* Fixed undefined format function when calling one of the built in validators form within a method validator. Fixes #98
883+
* 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.
877884
878885
#### v0.8.2 [commits](https://github.com/thedersen/backbone.validation/compare/v0.8.1...v0.8.2)
879886

dist/backbone-validation-amd-min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/backbone-validation-amd.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,11 @@
430430
rangeLength: '{0} must be between {1} and {2} characters',
431431
oneOf: '{0} must be one of: {1}',
432432
equalTo: '{0} must be the same as {1}',
433-
pattern: '{0} must be a valid {1}'
433+
digits: '{0} must only contain digits',
434+
number: '{0} must be a number',
435+
email: '{0} must be a valid email',
436+
url: '{0} must be a valid url',
437+
inlinePattern: '{0} is invalid'
434438
};
435439

436440
// Label formatters
@@ -621,7 +625,7 @@
621625
// Can be a regular expression or the name of one of the built in patterns
622626
pattern: function(value, attr, pattern, model) {
623627
if (!hasValue(value) || !value.toString().match(defaultPatterns[pattern] || pattern)) {
624-
return this.format(defaultMessages.pattern, this.formatLabel(attr, model), pattern);
628+
return this.format(defaultMessages[pattern] || defaultMessages.inlinePattern, this.formatLabel(attr, model), pattern);
625629
}
626630
}
627631
};

dist/backbone-validation-min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/backbone-validation.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,11 @@ Backbone.Validation = (function(_){
423423
rangeLength: '{0} must be between {1} and {2} characters',
424424
oneOf: '{0} must be one of: {1}',
425425
equalTo: '{0} must be the same as {1}',
426-
pattern: '{0} must be a valid {1}'
426+
digits: '{0} must only contain digits',
427+
number: '{0} must be a number',
428+
email: '{0} must be a valid email',
429+
url: '{0} must be a valid url',
430+
inlinePattern: '{0} is invalid'
427431
};
428432

429433
// Label formatters
@@ -614,7 +618,7 @@ Backbone.Validation = (function(_){
614618
// Can be a regular expression or the name of one of the built in patterns
615619
pattern: function(value, attr, pattern, model) {
616620
if (!hasValue(value) || !value.toString().match(defaultPatterns[pattern] || pattern)) {
617-
return this.format(defaultMessages.pattern, this.formatLabel(attr, model), pattern);
621+
return this.format(defaultMessages[pattern] || defaultMessages.inlinePattern, this.formatLabel(attr, model), pattern);
618622
}
619623
}
620624
};

src/backbone-validation.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,11 @@ Backbone.Validation = (function(_){
416416
rangeLength: '{0} must be between {1} and {2} characters',
417417
oneOf: '{0} must be one of: {1}',
418418
equalTo: '{0} must be the same as {1}',
419-
pattern: '{0} must be a valid {1}'
419+
digits: '{0} must only contain digits',
420+
number: '{0} must be a number',
421+
email: '{0} must be a valid email',
422+
url: '{0} must be a valid url',
423+
inlinePattern: '{0} is invalid'
420424
};
421425

422426
// Label formatters
@@ -607,7 +611,7 @@ Backbone.Validation = (function(_){
607611
// Can be a regular expression or the name of one of the built in patterns
608612
pattern: function(value, attr, pattern, model) {
609613
if (!hasValue(value) || !value.toString().match(defaultPatterns[pattern] || pattern)) {
610-
return this.format(defaultMessages.pattern, this.formatLabel(attr, model), pattern);
614+
return this.format(defaultMessages[pattern] || defaultMessages.inlinePattern, this.formatLabel(attr, model), pattern);
611615
}
612616
}
613617
};

tests/validators/pattern.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ buster.testCase("pattern validator", {
3434
this.model.set({email:''}, {validate: true});
3535
},
3636

37+
"has default error message for inline pattern": function(done) {
38+
this.model.bind('validated:invalid', function(model, error){
39+
assert.equals({name: 'Name is invalid'}, error);
40+
done();
41+
});
42+
this.model.set({name:''}, {validate: true});
43+
},
44+
3745
"value not matching pattern is invalid": function() {
3846
refute(this.model.set({
3947
name: 'aaa'

0 commit comments

Comments
 (0)