jQuery-Form-Validator icon indicating copy to clipboard operation
jQuery-Form-Validator copied to clipboard

data-validation-optional-if-answered does not handle case of duplicate input name in another form.

Open WingGithub opened this issue 7 years ago • 3 comments

If I have 2 different forms with input fields with the same names. The data-validation-optional-if-answered validator gets confused when there are such duplicates. Isn't it supposed to look at the named input of it's own form or is this by design?

WingGithub avatar Oct 05 '17 06:10 WingGithub

Looking at the code it should do exactly as you say, it should only try to find the referred input within the same form. Do you have any example code I can look at?

victorjonsson avatar Oct 05 '17 08:10 victorjonsson

It will take some to to extract sample working code from the project as I'm letting the backend do validation and the priority is deliverables. Debugging into the code, there is four forms with the same input names, each in a bootstrap modal that is hidden till needed. Is there a way to load non-minimized logic.js? I can't tell what is going on but it seems the formUtils.getValue() calls in logic.js gets a list of the four forms, along with all the other forms in the page and gets the input from first form of the four.

WingGithub avatar Oct 09 '17 03:10 WingGithub

Please look for comment below at the a.formUtils.getValue() /** File generated by Grunt -- do not modify <---- logic.js

  • JQUERY-FORM-VALIDATOR

  • @version 2.3.77

  • @website http://formvalidator.net/

  • @author Victor Jonsson, http://victorjonsson.se

  • @license MIT */ !function(a, b) { "function" == typeof define && define.amd ? define(["jquery"], function(a) { return b(a) }) : "object" == typeof module && module.exports ? module.exports = b(require("jquery")) : b(a.jQuery) }(this, function(a) { !function(a) { "use strict"; a.formUtils.registerLoadedModule("logic"); var b = function(b, c) { var d = function() { var c = a(this) , d = c.valAttr("depends-on") || c.valAttr("if-checked"); if (d) { var f = a.formUtils.getValue('[name="' + d + '"]', b) //<----- b gets a list of all the forms in my app. Comment continued in the code for getValue below: , g = a.split(c.valAttr("depends-on-value"), !1, !1) , h = !f || g.length && !e(f, g); h && c.valAttr("skipped", "1") } }

    getValue: function(query, $parent) { var $inputs = $parent ? $parent.find(query) : query; if ($inputs.length > 0 ) { var type = $inputs.eq(0).attr('type'); // <---- $inputs is the list of inputs from all the forms that has the specified name of the dependent input. I think the eq(0) get the first from this list regardless of whether this input is the one from the current form. Thats why the validation gets marked as skipped even when it is not supposed to be. if (type === 'radio' || type === 'checkbox') { return $inputs.filter(':checked').val() || ''; } else { return $inputs.val() || ''; } } return false;

This sequence of events is triggered when the modal is displayed, begining at line 1459 of the main https://cdnjs.cloudflare.com/ajax/libs/jquery-form-validator/2.3.77/jquery.form-validator.js script: .trigger('beforeValidation', [value, language, conf]);

WingGithub avatar Oct 17 '17 11:10 WingGithub