Skip to content

Commit

Permalink
feat(form): deprecated and rename empty to notEmpty rule
Browse files Browse the repository at this point in the history
This PR renames the empty rule to notEmpy but keeps the empty rule working for backward compatibility, so people can just upgrade their code for while. A console warning will tell them about.
Reasons for renaming is that the code actually checks if a field is not empty.
  • Loading branch information
lubber-de authored Dec 2, 2023
1 parent 392300a commit 0224737
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions src/definitions/behaviors/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,13 @@
fullFields[name].rules.push({ type: rule });
});
}

$.each(fullFields[name].rules, function (index, rule) {
var ruleName = module.get.ruleName(rule);
if (ruleName === 'empty') {
module.warn('*** DEPRECATED *** : Rule "empty" for field "' + name + '" will be removed in a future version. -> Use "notEmpty" rule instead.');
}
});
});

return fullFields;
Expand Down Expand Up @@ -591,10 +598,10 @@
},
settings: function () {
if ($.isPlainObject(parameters)) {
if (parameters.fields) {
parameters.fields = module.get.fieldsFromShorthand(parameters.fields);
}
settings = $.extend(true, {}, $.fn.form.settings, parameters);
if (settings.fields) {
settings.fields = module.get.fieldsFromShorthand(settings.fields);
}
validation = $.extend(true, {}, $.fn.form.settings.defaults, settings.fields);
module.verbose('Extending settings', validation, settings);
} else {
Expand Down Expand Up @@ -1237,20 +1244,20 @@
isRequired = $el.prop('required') || $elGroup.hasClass(className.required) || $elGroup.parent().hasClass(className.required),
isDisabled = $el.is(':disabled') || $elGroup.hasClass(className.disabled) || $elGroup.parent().hasClass(className.disabled),
validation = module.get.validation($el),
hasEmptyRule = validation
hasNotEmptyRule = validation
? $.grep(validation.rules, function (rule) {
return rule.type === 'empty';
}) !== 0
return ['notEmpty', 'checked', 'empty'].indexOf(rule.type) >= 0;
}).length > 0
: false,
identifier = module.get.identifier(validation, $el)
;
if (isRequired && !isDisabled && !hasEmptyRule && identifier !== undefined) {
if (isRequired && !isDisabled && !hasNotEmptyRule && 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');
module.verbose("Adding 'notEmpty' rule on field", identifier);
module.add.rule(identifier, 'notEmpty');
}
}
});
Expand Down Expand Up @@ -1492,6 +1499,12 @@
module.error.apply(console, arguments);
}
},
warn: function () {
if (!settings.silent) {
module.warn = Function.prototype.bind.call(console.warn, console, settings.name + ':');
module.warn.apply(console, arguments);
}
},
performance: {
log: function (message) {
var
Expand Down Expand Up @@ -1606,6 +1619,7 @@
name: 'Form',
namespace: 'form',

silent: false,
debug: false,
verbose: false,
performance: true,
Expand Down Expand Up @@ -1671,6 +1685,7 @@
maxValue: '{name} must have a maximum value of {ruleValue}',
minValue: '{name} must have a minimum value of {ruleValue}',
empty: '{name} must have a value',
notEmpty: '{name} must have a value',
checked: '{name} must be checked',
email: '{name} must be a valid e-mail',
url: '{name} must be a valid url',
Expand Down Expand Up @@ -1803,10 +1818,15 @@
rules: {

// is not empty or blank string
empty: function (value) {
notEmpty: function (value) {
return !(value === undefined || value === '' || (Array.isArray(value) && value.length === 0));
},

/* Deprecated */
empty: function (value) {
return $.fn.form.settings.rules.notEmpty(value);
},

// checkbox checked
checked: function () {
return $(this).filter(':checked').length > 0;
Expand Down

0 comments on commit 0224737

Please sign in to comment.