Skip to content

Commit

Permalink
feat(form): auto add empty rule validation for required fields
Browse files Browse the repository at this point in the history
This PR adds the ability to add automatically an 'empty' rule for fields that got the required class (or checked for checkboxes). It's disabled by default since I don't want it to be a breaking change, but can be enabled at initialization with autoCheckRequired: true or any time with the .form('set auto check') behavior.

It can handle all fields types, grouped fields as well, and is able to detect disabled fields or groups. Rule is added to others if there's already some defined, and isn't duplicated if already set.
  • Loading branch information
prudho authored Jan 30, 2020
1 parent 1356f15 commit ca80dca
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions src/definitions/behaviors/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ $.fn.form = function(parameters) {
module.verbose('Initializing form validation', $module, settings);
module.bindEvents();
module.set.defaults();
if (settings.autoCheckRequired) {
module.set.autoCheck();
}
module.instantiate();
}
},
Expand Down Expand Up @@ -1116,6 +1119,32 @@ $.fn.form = function(parameters) {
asDirty: function() {
module.set.defaults();
module.set.dirty();
},
autoCheck: function() {
module.debug('Enabling auto check on required fields');
$field.each(function (_index, el) {
var
$el = $(el),
$elGroup = $(el).closest($group),
isCheckbox = ($el.filter(selector.checkbox).length > 0),
isRequired = $el.prop('required') || $elGroup.hasClass(className.required) || $elGroup.parent().hasClass(className.required),
isDisabled = $el.prop('disabled') || $elGroup.hasClass(className.disabled) || $elGroup.parent().hasClass(className.disabled),
validation = module.get.validation($el),
hasEmptyRule = validation
? $.grep(validation.rules, function(rule) { return rule.type == "empty" }) !== 0
: false,
identifier = validation.identifier || $el.attr('id') || $el.attr('name') || $el.data(metadata.validate)
;
if (isRequired && !isDisabled && !hasEmptyRule && identifier !== undefined) {
if (isCheckbox) {
module.verbose("Adding 'checked' rule on field", identifier);
module.add.rule(identifier, "checked");
} else {
module.verbose("Adding 'empty' rule on field", identifier);
module.add.rule(identifier, "empty");
}
}
});
}
},

Expand Down Expand Up @@ -1455,6 +1484,7 @@ $.fn.form.settings = {
transition : 'scale',
duration : 200,

autoCheckRequired : false,
preventLeaving : false,
dateHandling : 'date', // 'date', 'input', 'formatter'

Expand Down Expand Up @@ -1535,10 +1565,12 @@ $.fn.form.settings = {
},

className : {
error : 'error',
label : 'ui basic red pointing prompt label',
pressed : 'down',
success : 'success'
error : 'error',
label : 'ui basic red pointing prompt label',
pressed : 'down',
success : 'success',
required : 'required',
disabled : 'disabled'
},

error: {
Expand Down

0 comments on commit ca80dca

Please sign in to comment.