Skip to content

Commit

Permalink
fix(form validation): make sure rules are selecting within the same form
Browse files Browse the repository at this point in the history
When there are multiple forms on a page and the form fields also have identical names, the `match` and `difference` rule functions possibly select the wrong field from a different form.
This PR makes sure the field selection takes place within the form
  • Loading branch information
lubber-de authored and Sean committed Apr 12, 2019
1 parent 7f63e50 commit 313012d
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/definitions/behaviors/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ $.fn.form = function(parameters) {
? ''
: (settings.shouldTrim) ? $.trim(value + '') : String(value + '')
;
return ruleFunction.call(field, value, ancillary);
return ruleFunction.call(field, value, ancillary, $module);
}
;
if( !$.isFunction(ruleFunction) ) {
Expand Down Expand Up @@ -1670,21 +1670,22 @@ $.fn.form.settings = {
},

// matches another field
match: function(value, identifier) {
match: function(value, identifier, $module) {
var
matchingValue
matchingValue,
matchingElement
;
if( $('[data-validate="'+ identifier +'"]').length > 0 ) {
matchingValue = $('[data-validate="'+ identifier +'"]').val();
if((matchingElement = $module.find('[data-validate="'+ identifier +'"]')).length > 0 ) {
matchingValue = matchingElement.val();
}
else if($('#' + identifier).length > 0) {
matchingValue = $('#' + identifier).val();
else if((matchingElement = $module.find('#' + identifier)).length > 0) {
matchingValue = matchingElement.val();
}
else if($('[name="' + identifier +'"]').length > 0) {
matchingValue = $('[name="' + identifier + '"]').val();
else if((matchingElement = $module.find('[name="' + identifier +'"]')).length > 0) {
matchingValue = matchingElement.val();
}
else if( $('[name="' + identifier +'[]"]').length > 0 ) {
matchingValue = $('[name="' + identifier +'[]"]');
else if((matchingElement = $module.find('[name="' + identifier +'[]"]')).length > 0 ) {
matchingValue = matchingElement;
}
return (matchingValue !== undefined)
? ( value.toString() == matchingValue.toString() )
Expand All @@ -1693,22 +1694,23 @@ $.fn.form.settings = {
},

// different than another field
different: function(value, identifier) {
different: function(value, identifier, $module) {
// use either id or name of field
var
matchingValue
matchingValue,
matchingElement
;
if( $('[data-validate="'+ identifier +'"]').length > 0 ) {
matchingValue = $('[data-validate="'+ identifier +'"]').val();
if((matchingElement = $module.find('[data-validate="'+ identifier +'"]')).length > 0 ) {
matchingValue = matchingElement.val();
}
else if($('#' + identifier).length > 0) {
matchingValue = $('#' + identifier).val();
else if((matchingElement = $module.find('#' + identifier)).length > 0) {
matchingValue = matchingElement.val();
}
else if($('[name="' + identifier +'"]').length > 0) {
matchingValue = $('[name="' + identifier + '"]').val();
else if((matchingElement = $module.find('[name="' + identifier +'"]')).length > 0) {
matchingValue = matchingElement.val();
}
else if( $('[name="' + identifier +'[]"]').length > 0 ) {
matchingValue = $('[name="' + identifier +'[]"]');
else if((matchingElement = $module.find('[name="' + identifier +'[]"]')).length > 0 ) {
matchingValue = matchingElement;
}
return (matchingValue !== undefined)
? ( value.toString() !== matchingValue.toString() )
Expand Down

0 comments on commit 313012d

Please sign in to comment.