Skip to content

Commit 99a3e5d

Browse files
committed
Added patch for label support from ansman#102
1 parent f44c89b commit 99a3e5d

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

specs/validate-helpers-spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,36 @@ describe("validate", function() {
433433
}]);
434434
});
435435

436+
it("use the attributes label if it's specified in options", function() {
437+
var errors = [{
438+
attribute: "foo",
439+
error: "can't be blank",
440+
someOtherProperty: "someOtherProperty",
441+
value: "foobar"
442+
}, {
443+
attribute: "foo_bar",
444+
error: "has some other problem",
445+
value: "foobar"
446+
}];
447+
448+
var options = {
449+
labels: {
450+
foo: 'foo label'
451+
}
452+
};
453+
454+
expect(convertErrorMessages(errors, options)).toEqual([{
455+
attribute: "foo",
456+
error: "Foo label can't be blank",
457+
someOtherProperty: "someOtherProperty",
458+
value: "foobar"
459+
}, {
460+
attribute: "foo_bar",
461+
error: "Foo bar has some other problem",
462+
value: "foobar"
463+
}]);
464+
});
465+
436466
it("doesn't modify the input", function() {
437467
var errors = [{
438468
attribute: "foo",

validate.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,14 @@
438438
return v.prettify(value);
439439
},
440440

441+
attributeLabel: function(attribute, options) {
442+
var label = v.isObject(options) &&
443+
v.isObject(options.labels) &&
444+
options.labels[attribute];
445+
446+
return label || v.prettify(attribute);
447+
},
448+
441449
isString: function(value) {
442450
return typeof value === 'string';
443451
},
@@ -652,7 +660,8 @@
652660
if (error[0] === '^') {
653661
error = error.slice(1);
654662
} else if (options.fullMessages !== false) {
655-
error = v.capitalize(v.prettify(errorInfo.attribute)) + " " + error;
663+
var label = v.attributeLabel(errorInfo.attribute, options);
664+
error = v.capitalize(label) + " " + error;
656665
}
657666
error = error.replace(/\\\^/g, "^");
658667
error = v.format(error, {value: v.stringifyValue(errorInfo.value)});
@@ -1034,7 +1043,7 @@
10341043
}, {
10351044
PATTERN: /^[a-z0-9\u007F-\uffff!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9\u007F-\uffff!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$/i
10361045
}),
1037-
equality: function(value, options, attribute, attributes) {
1046+
equality: function(value, options, attribute, attributes, globalOptions) {
10381047
if (v.isEmpty(value)) {
10391048
return;
10401049
}
@@ -1057,7 +1066,7 @@
10571066
};
10581067

10591068
if (!comparator(value, otherValue, options, attribute, attributes)) {
1060-
return v.format(message, {attribute: v.prettify(options.attribute)});
1069+
return v.format(message, {attribute: v.attributeLabel(options.attribute, globalOptions)});
10611070
}
10621071
},
10631072

0 commit comments

Comments
 (0)