ampersand-form-view
ampersand-form-view copied to clipboard
Easy to to get list of invalid form fields
Ran into an issue recently with an invalid form field preventing the form from submitting. Right now an invalid class is added to an element, but it would be nice to have a method on the class to get a list of invalid form fields.
var invalidFields = formView.invalidFields();
@scottcorgan, simple enough. You can extend with something like
invalidFields: function() {
return this._fieldViewsArray.filter(function(field) {
return !field.valid;
});
};
will that work for ya?
I went a different way and changed the setValid function so validCallback gets a second parameter:
setValid: function (now, forceFire) {
var prev = this.valid;
this.valid = now;
if (prev !== now || forceFire) {
this.validCallback(now, _.mapValues(this._fieldViews, 'valid'));
}
},
Now, my valid callback gets:
- A Boolean- Whether everything is valid or not.
- An Object- Keys are input names and values are valid or not, e.g.
{ 'firstName': true, 'lastName': false }