Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

add attribute label support #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions specs/validate-helpers-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,36 @@ describe("validate", function() {
}]);
});

it("use the attributes label if it's specified in options", function() {
var errors = [{
attribute: "foo",
error: "can't be blank",
someOtherProperty: "someOtherProperty",
value: "foobar"
}, {
attribute: "foo_bar",
error: "has some other problem",
value: "foobar"
}];

var options = {
labels: {
foo: 'foo label'
}
};

expect(convertErrorMessages(errors, options)).toEqual([{
attribute: "foo",
error: "Foo label can't be blank",
someOtherProperty: "someOtherProperty",
value: "foobar"
}, {
attribute: "foo_bar",
error: "Foo bar has some other problem",
value: "foobar"
}]);
});

it("doesn't modify the input", function() {
var errors = [{
attribute: "foo",
Expand Down
15 changes: 12 additions & 3 deletions validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,14 @@
.toLowerCase();
},

attributeLabel: function(attribute, options) {
var label = v.isObject(options) &&
v.isObject(options.labels) &&
options.labels[attribute];

return label || v.prettify(attribute);
},

stringifyValue: function(value) {
return v.prettify(value);
},
Expand Down Expand Up @@ -641,7 +649,8 @@
if (error[0] === '^') {
error = error.slice(1);
} else if (options.fullMessages !== false) {
error = v.capitalize(v.prettify(errorInfo.attribute)) + " " + error;
var label = v.attributeLabel(errorInfo.attribute, options);
error = v.capitalize(label) + " " + error;
}
error = error.replace(/\\\^/g, "^");
error = v.format(error, {value: v.stringifyValue(errorInfo.value)});
Expand Down Expand Up @@ -1009,7 +1018,7 @@
}, {
PATTERN: /^[a-z0-9\u007F-\uffff!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9\u007F-\uffff!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$/i
}),
equality: function(value, options, attribute, attributes) {
equality: function(value, options, attribute, attributes, globalOptions) {
if (v.isEmpty(value)) {
return;
}
Expand All @@ -1032,7 +1041,7 @@
};

if (!comparator(value, otherValue, options, attribute, attributes)) {
return v.format(message, {attribute: v.prettify(options.attribute)});
return v.format(message, {attribute: v.attributeLabel(options.attribute, globalOptions)});
}
},

Expand Down