Skip to content

Commit 9eb7c33

Browse files
committed
Allow recursive error
1 parent 359bac1 commit 9eb7c33

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

addon/components/validatable-form.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,38 @@ export default Ember.Component.extend({
7171

7272
// For now, we assume that there are "id" properly set and that they match the attribute name
7373
errors.forEach(function(item) {
74-
var attribute = Ember.String.dasherize(item.attribute),
75-
element = Ember.$.find('#' + attribute);
76-
77-
if (element.length > 0) {
78-
element[0].setCustomValidity(item.message);
79-
}
80-
});
74+
this.renderServerError(item.attribute, item.message);
75+
}, this);
8176

8277
// Force validation of the form
8378
this.get('element').checkValidity();
8479
this.scrollToFirstError();
8580
}.observes('model.errors.[]'),
8681

82+
/**
83+
* @param {String} item
84+
* @param {String|Object} message
85+
*/
86+
renderServerError: function(item, message) {
87+
var attribute = Ember.String.dasherize(item),
88+
messageType = Ember.typeOf(message);
89+
90+
// If message is itself an object, this means it is a nested error
91+
if (messageType === 'object') {
92+
for (var key in message) {
93+
if (message.hasOwnProperty(key)) {
94+
this.renderServerError(item + '[' + key + ']', message[key]);
95+
}
96+
}
97+
} else {
98+
var element = Ember.$.find('#' + attribute.replace(/(:|\.|\[|\]|,)/g, '\\$1'));
99+
100+
if (element.length > 0) {
101+
element[0].setCustomValidity(messageType === 'array' ? message[0] : message);
102+
}
103+
}
104+
},
105+
87106
/**
88107
* Scroll to the first input field that does not pass the validation
89108
*

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ember-cli-html5-validation",
3-
"version": "0.0.13",
3+
"version": "0.0.14",
44
"directories": {
55
"doc": "doc",
66
"test": "tests"

0 commit comments

Comments
 (0)