JQuery.validate require_from_group

Whenever I use require_from_group , it disables all other checks. Any ideas why?

Is there also a way to group Telefon and Mobitel and apply require_from_group to it?

  $(document).ready(function(){ $("#fncMain").validate( { /*groups:{Call:"Telefon Mobitel"},*/ rules:{ Davcna:{required:true,exactlength:5, digits:true}, Idzav:{required:true,exactlength:5, digits:true}, Maticna:{required:true,exactlength:5, digits:true}, Telefon:{require_from_group: [1,".callme"]}, Mobitel:{require_from_group: [1,".callme"]} }, messages:{ }} ); }); 

All other fields that are not included here use the simple "necessary" class. If I remove the require_from_group rules that apply to Telefon and Mobitel, all other field checks work fine.

Thanks for the help.

EDIT html: http://cl.ly/29391q0Q3G231T2I380m (too long to post it here)

+6
source share
2 answers

@Tats_innit posted a custom require_from_group here: https://stackoverflow.com/posts/13127475

It turns out that this also fixes a registered github bug that was released with version 1.10.0 from require_from_group in additional-method-1.10.js for jquery.validation .

github problem: require_from_group disables other rules

smileyanp @github cited this post in his decision, where he reused the @Tats_innit function and created a test that showed that it works correctly and does not disable the check for other rules defined before require_from_group .

This post is here as a temporary splash since it burned 3 hours of a search engine for such a small detail.

FIX:

Just update additional-method-1.10.js or execute this code after loading additional-method-1.10.js (to overwrite the function).

 jQuery.validator.addMethod("require_from_group", function(value, element, options) { var numberRequired = options[0]; var selector = options[1]; var fields = $(selector, element.form); var filled_fields = fields.filter(function() { // it more clear to compare with empty string return $(this).val() != ""; }); var empty_fields = fields.not(filled_fields); // we will mark only first empty field as invalid if (filled_fields.length < numberRequired && empty_fields[0] == element) { return false; } return true; // {0} below is the 0th item in the options field }, jQuery.format("Please fill out at least {0} of these fields.")); 
+5
source

Change It actually looks like this fix was included in version 1.12.0, and you can find the CDN pointers here: http://jqueryvalidation.org/

And for reference:

 http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.js http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.min.js http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/additional-methods.js http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/additional-methods.min.js 

I found this code below before I found the solution above, so I suggest using the CDN links mentioned above instead of pasting the code below into your JS file.

There is a better fix on GitHub now (scroll to the bottom) that I copied here. This is not my job , and the sfreytag user from GitHub who wrote it is not a sponsor of SO, I just wanted to get it in SO, so other people who find this do not need to dig through streams on GitHub:

 jQuery.validator.addMethod("require_from_group", function(value, element, options) { var validator = this; var selector = options[1]; var validOrNot = $(selector, element.form).filter(function() { return validator.elementValue(this); }).length >= options[0]; if(!$(element).data('being_validated')) { var fields = $(selector, element.form); fields.data('being_validated', true); fields.valid(); $(element.form).valid(); fields.data('being_validated', false); } return validOrNot; }, jQuery.format("Please fill at least {0} of these fields.")); 

I have done some limited testing so far, but it seems to work the way you expected, all the checks (instead of having to do any โ€œrequire_from_groupโ€ checks as before), so I'm happy with it so far. I just added it after the validator declaration at the top of my JS code:

 $.validator.setDefaults({ debug: true, success: "valid" }); jQuery.validator.addMethod("require_from_group", function(value, element, options) { var validator = this; var selector = options[1]; //continuation of code... 
+2
source

Source: https://habr.com/ru/post/915014/


All Articles