简体   繁体   English

Jquery .validate require_from_group

[英]Jquery .validate require_from_group

Whenever i use require_from_group it disables all other validations. 每当我使用require_from_group它都会禁用所有其他验证。 Any ideas why? 有什么想法吗?

Also is there a way to group "Telefon" and "Mobitel" and apply require_from_group to it? 还有一种方法可以将“Telefon”和“Mobitel”分组并对其应用require_from_group吗?

  $(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. 如果我删除了应用于“Telefon”和“Mobitel”的require_from_group规则,则所有其他字段验证都可以正常工作。

Thanks for help. 感谢帮助。

EDIT html : http://cl.ly/29391q0Q3G231T2I380m (too long to post it here) 编辑 html: http//cl.ly/29391q0Q3G231T2I380m (太长了,不能在这里发布)

@Tats_innit posted a custom require_from_group here: https://stackoverflow.com/posts/13127475 @Tats_innit在这里发布了一个自定义的require_from_grouphttps ://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 . 事实证明,这也解决了与的1.10.0版本发布的登录GitHub的错误require_from_groupadditional-method-1.10.jsjquery.validation

github issue: require_from_group disables other rules github问题: require_from_group禁用其他规则

smileyanp@github 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 . smileyanp @ github在他的解决方案中引用了这篇文章,他重用了@ Tats_innit的函数并创建了一个测试 ,证明它可以正常工作,并且不会禁用require_from_group之前定义的其他规则的验证。

This post is here as a time saver as this burned 3 hrs of googling for such a small detail.. 这篇帖子可以节省时间,因为这会花费3小时的谷歌搜索这么小的细节。

FIX: 固定:

Just update additional-method-1.10.js or execute this code after additional-method-1.10.js has loaded (to overwrite the function). 只需更新additional-method-1.10.js或在加载additional-method-1.10.js后执行此代码(覆盖该函数)。

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."));

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/ 编辑 :实际上看起来这个修补程序已经合并到版本1.12.0中,你可以在这里找到它的CDN指针: 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. 在我找到上面的解决方案之前,我在下面找到了这个代码,所以我的建议是使用上面引用的CDN链接,而不是将下面的代码粘贴到JS文件中。

There is a better fix out on GitHub now (scroll to the very bottom), which I've copied here. 现在有一个更好的修复GitHub (滚动到最底部),我在这里复制了。 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: 这不是我的工作 ,编写它的GitHub用户sfreytag,似乎不是SO贡献者,我只是想把它变成SO所以其他发现这个的人不必在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. 到目前为止,我已经对此进行了有限的测试,但它似乎正如您所期望的那样工作,所有验证都会发生(而不是像以前那样吹过任何非“require_from_group”验证),所以到目前为止我很满意。 I just added it after the the validator declaration in the top of my JS code: 我刚刚在我的JS代码顶部的验证器声明之后添加它:

$.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...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM