Jquery .validate require_from_group

Whenever i use require_from_group it disables all other validations. Any ideas why?

Also is there 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 not included here use simple "required" class. If i remove require_from_group rules applied to "Telefon" and "Mobitel" all other field validations work fine.

Thanks for help.

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

Answers:

Answer

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

Turns out, this also fixes a logged github bug that was released with version 1.10.0 of require_from_group in additional-method-1.10.js for jquery.validation.

github issue: require_from_group disables other rules

[email protected] quoted this post in his solution where he reused @Tats_innit's function and created a test that showed it works correctly and doesn't disable validation on other rules defined prior to the require_from_group.

This post is here as a time saver as this burned 3 hrs of googling for such a small detail..

FIX:

Just update additional-method-1.10.js or execute this code after additional-method-1.10.js has loaded (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's 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."));
Answer

Edit: It actually looks like this fix has been incorporated into version 1.12.0 and you can find the CDN pointers for it 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 my advice is to use the CDN links referenced above instead of pasting the code below into your JS file.

There is a better fix out on GitHub now (scroll to the very bottom), which I've copied here. This is not my work and the GitHub user sfreytag who wrote it, does not appear to be an SO contributor, I just wanted to get it into SO so other people who find this don't have to dig through threads 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 limited testing with this thus far, but it appears to be working as you'd expect, all validations occur (instead of blowing through any non "require_from_group" validations as before), so I'm happy with it so far. I just added it after the the validator declaration in 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...

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.