From ca80dcabcbb8433b5feb6e9dd8d2cfcfd7b2e15e Mon Sep 17 00:00:00 2001 From: prudho Date: Thu, 30 Jan 2020 13:13:56 +0100 Subject: [PATCH] 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. --- src/definitions/behaviors/form.js | 40 +++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/definitions/behaviors/form.js b/src/definitions/behaviors/form.js index b71df56e4d..b365ca0591 100644 --- a/src/definitions/behaviors/form.js +++ b/src/definitions/behaviors/form.js @@ -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(); } }, @@ -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"); + } + } + }); } }, @@ -1455,6 +1484,7 @@ $.fn.form.settings = { transition : 'scale', duration : 200, + autoCheckRequired : false, preventLeaving : false, dateHandling : 'date', // 'date', 'input', 'formatter' @@ -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: {