Skip to content

Commit ca80dca

Browse files
authored
feat(form): auto add empty rule validation for required fields
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.
1 parent 1356f15 commit ca80dca

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

src/definitions/behaviors/form.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ $.fn.form = function(parameters) {
9595
module.verbose('Initializing form validation', $module, settings);
9696
module.bindEvents();
9797
module.set.defaults();
98+
if (settings.autoCheckRequired) {
99+
module.set.autoCheck();
100+
}
98101
module.instantiate();
99102
}
100103
},
@@ -1116,6 +1119,32 @@ $.fn.form = function(parameters) {
11161119
asDirty: function() {
11171120
module.set.defaults();
11181121
module.set.dirty();
1122+
},
1123+
autoCheck: function() {
1124+
module.debug('Enabling auto check on required fields');
1125+
$field.each(function (_index, el) {
1126+
var
1127+
$el = $(el),
1128+
$elGroup = $(el).closest($group),
1129+
isCheckbox = ($el.filter(selector.checkbox).length > 0),
1130+
isRequired = $el.prop('required') || $elGroup.hasClass(className.required) || $elGroup.parent().hasClass(className.required),
1131+
isDisabled = $el.prop('disabled') || $elGroup.hasClass(className.disabled) || $elGroup.parent().hasClass(className.disabled),
1132+
validation = module.get.validation($el),
1133+
hasEmptyRule = validation
1134+
? $.grep(validation.rules, function(rule) { return rule.type == "empty" }) !== 0
1135+
: false,
1136+
identifier = validation.identifier || $el.attr('id') || $el.attr('name') || $el.data(metadata.validate)
1137+
;
1138+
if (isRequired && !isDisabled && !hasEmptyRule && identifier !== undefined) {
1139+
if (isCheckbox) {
1140+
module.verbose("Adding 'checked' rule on field", identifier);
1141+
module.add.rule(identifier, "checked");
1142+
} else {
1143+
module.verbose("Adding 'empty' rule on field", identifier);
1144+
module.add.rule(identifier, "empty");
1145+
}
1146+
}
1147+
});
11191148
}
11201149
},
11211150

@@ -1455,6 +1484,7 @@ $.fn.form.settings = {
14551484
transition : 'scale',
14561485
duration : 200,
14571486

1487+
autoCheckRequired : false,
14581488
preventLeaving : false,
14591489
dateHandling : 'date', // 'date', 'input', 'formatter'
14601490

@@ -1535,10 +1565,12 @@ $.fn.form.settings = {
15351565
},
15361566

15371567
className : {
1538-
error : 'error',
1539-
label : 'ui basic red pointing prompt label',
1540-
pressed : 'down',
1541-
success : 'success'
1568+
error : 'error',
1569+
label : 'ui basic red pointing prompt label',
1570+
pressed : 'down',
1571+
success : 'success',
1572+
required : 'required',
1573+
disabled : 'disabled'
15421574
},
15431575

15441576
error: {

0 commit comments

Comments
 (0)