Skip to content

Commit

Permalink
fix(form): support mix of shorthand and full object rules
Browse files Browse the repository at this point in the history
It was not possible to mix shorthand and full object rules together (only the first one validatedc and then crashes at JS console).
  • Loading branch information
lubber-de authored Dec 24, 2020
1 parent 686529b commit d1b6dea
Showing 1 changed file with 14 additions and 26 deletions.
40 changes: 14 additions & 26 deletions src/definitions/behaviors/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,6 @@ $.fn.form = function(parameters) {
bracketedRule: function(rule) {
return (rule.type && rule.type.match(settings.regExp.bracket));
},
shorthandFields: function(fields) {
var
fieldKeys = Object.keys(fields),
firstRule = fields[fieldKeys[0]]
;
return module.is.shorthandRules(firstRule);
},
// duck type rule test
shorthandRules: function(rules) {
return (typeof rules == 'string' || Array.isArray(rules));
Expand Down Expand Up @@ -523,15 +516,19 @@ $.fn.form = function(parameters) {
fullFields = {}
;
$.each(fields, function(name, rules) {
if(typeof rules == 'string') {
rules = [rules];
if (!Array.isArray(rules) && typeof rules === 'object') {
fullFields[name] = rules;
} else {
if (typeof rules == 'string') {
rules = [rules];
}
fullFields[name] = {
rules: []
};
$.each(rules, function (index, rule) {
fullFields[name].rules.push({type: rule});
});
}
fullFields[name] = {
rules: []
};
$.each(rules, function(index, rule) {
fullFields[name].rules.push({ type: rule });
});
});
return fullFields;
},
Expand Down Expand Up @@ -599,7 +596,7 @@ $.fn.form = function(parameters) {
}
else {
// 2.x
if(parameters.fields && module.is.shorthandFields(parameters.fields)) {
if(parameters.fields) {
parameters.fields = module.get.fieldsFromShorthand(parameters.fields);
}
settings = $.extend(true, {}, $.fn.form.settings, parameters);
Expand Down Expand Up @@ -873,16 +870,7 @@ $.fn.form = function(parameters) {
module.debug('Adding rules', newValidation.rules, validation);
},
fields: function(fields) {
var
newValidation
;
if(fields && module.is.shorthandFields(fields)) {
newValidation = module.get.fieldsFromShorthand(fields);
}
else {
newValidation = fields;
}
validation = $.extend({}, validation, newValidation);
validation = $.extend({}, validation, module.get.fieldsFromShorthand(fields));
},
prompt: function(identifier, errors, internal) {
var
Expand Down

0 comments on commit d1b6dea

Please sign in to comment.