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: {