Skip to content

Commit

Permalink
fix(form validation): array validations should respect each single field
Browse files Browse the repository at this point in the history
If a form uses array inputs by appending brackets to their names ( `name="myfield[]"`) the validation on those fields was always done for the whole field group. Means, if the first field was valid, it validated for the whole group which is wrong.
This behavior is now fixed, so even if multiple input fields have the same name to act as array inputs, the validation is done on each field separately and also the error class is set/removed to each field individually.
@prudho was already providing a [PR for that in SUI](Semantic-Org/Semantic-UI#6370) some time ago, but the individual error display handling was missing there.

Hint: If you ask yourself while reviewing, why i added an `internal` parameter to the `add.prompt()`and `validate.rule()` functions: This is to make sure those functions are still callable  as behavior from separate JS (for example: `$('.foo').form('validate rule', argumentOne, argumentTwo)` so they do not break existing code
  • Loading branch information
lubber-de authored and Sean committed Jan 22, 2019
1 parent 88d0e99 commit 5be7315
Showing 1 changed file with 42 additions and 19 deletions.
61 changes: 42 additions & 19 deletions src/definitions/behaviors/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ $.fn.form = function(parameters) {
}
validation = $.extend({}, validation, newValidation);
},
prompt: function(identifier, errors) {
prompt: function(identifier, errors, internal) {
var
$field = module.get.field(identifier),
$fieldGroup = $field.closest($group),
Expand All @@ -716,9 +716,11 @@ $.fn.form = function(parameters) {
: errors
;
module.verbose('Adding field error state', identifier);
$fieldGroup
.addClass(className.error)
;
if(!internal) {
$fieldGroup
.addClass(className.error)
;
}
if(settings.inline) {
if(!promptExists) {
$prompt = settings.templates.prompt(errors);
Expand Down Expand Up @@ -997,11 +999,18 @@ $.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);
$.each(field.rules, function(index, rule) {
if( module.has.field(identifier) && !( module.validate.rule(field, rule) ) ) {
module.debug('Field is invalid', identifier, rule.type);
fieldErrors.push(module.get.prompt(rule, field));
fieldValid = false;
if( module.has.field(identifier)) {
var invalidFields = module.validate.rule(field, rule,true) || [];
if (invalidFields.length>0){
module.debug('Field is invalid', identifier, rule.type);
fieldErrors.push(module.get.prompt(rule, field));
fieldValid = false;
if(showErrors){
$(invalidFields).closest($group).addClass(className.error);
}
}
}
});
}
Expand All @@ -1014,7 +1023,7 @@ $.fn.form = function(parameters) {
else {
if(showErrors) {
formErrors = formErrors.concat(fieldErrors);
module.add.prompt(identifier, fieldErrors);
module.add.prompt(identifier, fieldErrors, true);
settings.onInvalid.call($field, fieldErrors);
}
return false;
Expand All @@ -1023,26 +1032,40 @@ $.fn.form = function(parameters) {
},

// takes validation rule and returns whether field passes rule
rule: function(field, rule) {
rule: function(field, rule, internal) {
var
$field = module.get.field(field.identifier),
type = rule.type,
value = $field.val(),
isValid = true,
ancillary = module.get.ancillaryValue(rule),
ruleName = module.get.ruleName(rule),
ruleFunction = settings.rules[ruleName]
ruleFunction = settings.rules[ruleName],
invalidFields = [],
isValid = function(field){
var value = $(field).val();
// cast to string avoiding encoding special values
value = (value === undefined || value === '' || value === null)
? ''
: (settings.shouldTrim) ? $.trim(value + '') : String(value + '')
;
return ruleFunction.call(field, value, ancillary);
}
;
if( !$.isFunction(ruleFunction) ) {
module.error(error.noRule, ruleName);
return;
}
// cast to string avoiding encoding special values
value = (value === undefined || value === '' || value === null)
? ''
: (settings.shouldTrim) ? $.trim(value + '') : String(value + '')
;
return ruleFunction.call($field, value, ancillary);
if($field.is(selector.radio)) {
if (!isValid($field)) {
invalidFields = $field;
}
} else {
$.each($field, function (index, field) {
if (!isValid(field)) {
invalidFields.push(field);
}
});
}
return internal ? invalidFields : !(invalidFields.length>0);
}
},

Expand Down

0 comments on commit 5be7315

Please sign in to comment.