Skip to content

Commit

Permalink
fix(form): isvalid should not delete error state and made optional now
Browse files Browse the repository at this point in the history
the is valid behavior deletes possible existing error states on a field, although it shouldn't.
That's because the field validator always removes the error state by default (before probably adding it again) and did not respect a showErrors setting. showErrors however is only meant to show new errors. If it is false it should not touch any existing error state class.

I now also added the showErrors parameter as an option to the is valid behavior in case someone definately wants to show errors on not already validated fields.

As the parameter is optional the change is fully backwards compatible.
  • Loading branch information
lubber-de authored Jul 31, 2020
1 parent c8ad8b9 commit 33397cc
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/definitions/behaviors/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,18 +338,18 @@ $.fn.form = function(parameters) {
blank: function($field) {
return String($field.val()).trim() === '';
},
valid: function(field) {
valid: function(field, showErrors) {
var
allValid = true
;
if(field) {
module.verbose('Checking if field is valid', field);
return module.validate.field(validation[field], field, false);
return module.validate.field(validation[field], field, !!showErrors);
}
else {
module.verbose('Checking if form is valid');
$.each(validation, function(fieldName, field) {
if( !module.is.valid(fieldName) ) {
if( !module.is.valid(fieldName, showErrors) ) {
allValid = false;
}
});
Expand Down Expand Up @@ -1235,7 +1235,9 @@ $.fn.form = function(parameters) {
module.debug('Field depends on another value that is not present or empty. Skipping', $dependsField);
}
else if(field.rules !== undefined) {
$field.closest($group).removeClass(className.error);
if(showErrors) {
$field.closest($group).removeClass(className.error);
}
$.each(field.rules, function(index, rule) {
if( module.has.field(identifier)) {
var invalidFields = module.validate.rule(field, rule,true) || [];
Expand Down

0 comments on commit 33397cc

Please sign in to comment.