ampersand-form-view icon indicating copy to clipboard operation
ampersand-form-view copied to clipboard

Easy to to get list of invalid form fields

Open scottcorgan opened this issue 9 years ago • 2 comments

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 avatar Nov 05 '14 17:11 scottcorgan

@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?

cdaringe avatar Mar 25 '15 03:03 cdaringe

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 }

maddijoyce avatar Jul 22 '15 06:07 maddijoyce