Skip to content

Commit 0224737

Browse files
authored
feat(form): deprecated and rename empty to notEmpty rule
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.
1 parent 392300a commit 0224737

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

src/definitions/behaviors/form.js

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,13 @@
538538
fullFields[name].rules.push({ type: rule });
539539
});
540540
}
541+
542+
$.each(fullFields[name].rules, function (index, rule) {
543+
var ruleName = module.get.ruleName(rule);
544+
if (ruleName === 'empty') {
545+
module.warn('*** DEPRECATED *** : Rule "empty" for field "' + name + '" will be removed in a future version. -> Use "notEmpty" rule instead.');
546+
}
547+
});
541548
});
542549

543550
return fullFields;
@@ -591,10 +598,10 @@
591598
},
592599
settings: function () {
593600
if ($.isPlainObject(parameters)) {
594-
if (parameters.fields) {
595-
parameters.fields = module.get.fieldsFromShorthand(parameters.fields);
596-
}
597601
settings = $.extend(true, {}, $.fn.form.settings, parameters);
602+
if (settings.fields) {
603+
settings.fields = module.get.fieldsFromShorthand(settings.fields);
604+
}
598605
validation = $.extend(true, {}, $.fn.form.settings.defaults, settings.fields);
599606
module.verbose('Extending settings', validation, settings);
600607
} else {
@@ -1237,20 +1244,20 @@
12371244
isRequired = $el.prop('required') || $elGroup.hasClass(className.required) || $elGroup.parent().hasClass(className.required),
12381245
isDisabled = $el.is(':disabled') || $elGroup.hasClass(className.disabled) || $elGroup.parent().hasClass(className.disabled),
12391246
validation = module.get.validation($el),
1240-
hasEmptyRule = validation
1247+
hasNotEmptyRule = validation
12411248
? $.grep(validation.rules, function (rule) {
1242-
return rule.type === 'empty';
1243-
}) !== 0
1249+
return ['notEmpty', 'checked', 'empty'].indexOf(rule.type) >= 0;
1250+
}).length > 0
12441251
: false,
12451252
identifier = module.get.identifier(validation, $el)
12461253
;
1247-
if (isRequired && !isDisabled && !hasEmptyRule && identifier !== undefined) {
1254+
if (isRequired && !isDisabled && !hasNotEmptyRule && identifier !== undefined) {
12481255
if (isCheckbox) {
12491256
module.verbose("Adding 'checked' rule on field", identifier);
12501257
module.add.rule(identifier, 'checked');
12511258
} else {
1252-
module.verbose("Adding 'empty' rule on field", identifier);
1253-
module.add.rule(identifier, 'empty');
1259+
module.verbose("Adding 'notEmpty' rule on field", identifier);
1260+
module.add.rule(identifier, 'notEmpty');
12541261
}
12551262
}
12561263
});
@@ -1492,6 +1499,12 @@
14921499
module.error.apply(console, arguments);
14931500
}
14941501
},
1502+
warn: function () {
1503+
if (!settings.silent) {
1504+
module.warn = Function.prototype.bind.call(console.warn, console, settings.name + ':');
1505+
module.warn.apply(console, arguments);
1506+
}
1507+
},
14951508
performance: {
14961509
log: function (message) {
14971510
var
@@ -1606,6 +1619,7 @@
16061619
name: 'Form',
16071620
namespace: 'form',
16081621

1622+
silent: false,
16091623
debug: false,
16101624
verbose: false,
16111625
performance: true,
@@ -1671,6 +1685,7 @@
16711685
maxValue: '{name} must have a maximum value of {ruleValue}',
16721686
minValue: '{name} must have a minimum value of {ruleValue}',
16731687
empty: '{name} must have a value',
1688+
notEmpty: '{name} must have a value',
16741689
checked: '{name} must be checked',
16751690
email: '{name} must be a valid e-mail',
16761691
url: '{name} must be a valid url',
@@ -1803,10 +1818,15 @@
18031818
rules: {
18041819

18051820
// is not empty or blank string
1806-
empty: function (value) {
1821+
notEmpty: function (value) {
18071822
return !(value === undefined || value === '' || (Array.isArray(value) && value.length === 0));
18081823
},
18091824

1825+
/* Deprecated */
1826+
empty: function (value) {
1827+
return $.fn.form.settings.rules.notEmpty(value);
1828+
},
1829+
18101830
// checkbox checked
18111831
checked: function () {
18121832
return $(this).filter(':checked').length > 0;

0 commit comments

Comments
 (0)