【问题标题】:Jquery .validate require_from_groupJquery .validate 需要_from_group
【发布时间】:2012-05-06 10:43:38
【问题描述】:

每当我使用require_from_group 时,它都会禁用所有其他验证。任何想法为什么?

还有没有办法将“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:{

    }}
    );
  });

此处未包含的所有其他字段都使用简单的“必需”类。如果我删除应用于“Telefon”和“Mobitel”的require_from_group 规则,所有其他字段验证都可以正常工作。

感谢您的帮助。

编辑 html : http://cl.ly/29391q0Q3G231T2I380m(太长,无法在此处发布)

【问题讨论】:

  • 您能否分享此表单的 html,至少与这些规则有关?即类callme等有什么东西
  • @Ryley 我发布了我的 HTML(表单部分)。检查我的帖子的编辑。 :)
  • 我认为你发现了一个错误......
  • 有关潜在错误的详细信息,请参阅this。如果这最终成为解决方案,请将其作为答案发布在这里,并为我点赞:)
  • 呵呵,我实际上发布了这个错误报告 :) 感谢简化它;)

标签: javascript jquery validation


【解决方案1】:

@Tats_innit 在此处发布了自定义 require_from_grouphttps://stackoverflow.com/posts/13127475

事实证明,这还修复了一个已记录的 github 错误,该错误已在 additional-method-1.10.js 中的 require_from_group 版本 1.10.0 中为 jquery.validation 发布。

github 问题: require_from_group disables other rules

smileyanp@github 在他的解决方案中引用了这篇文章,他重用了@Tats_innit 的函数并创建了一个test,表明它可以正常工作并且不会禁用在@之前定义的其他规则的验证987654328@.

这篇文章在这里是为了节省时间,因为它花了 3 小时在谷歌上搜索这样一个小细节..

修复:

只需更新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."));

【讨论】:

  • 在将此函数的 1.11.1 版本的行为与此答案中的版本进行比较时,我注意到此版本似乎没有像发货版本那样进行贪婪的重新验证。这个版本确实在表单提交时重新验证,但是一旦最小数量的字段中有值(onkeyup),发货的就会清除验证消息。仅供参考,以防有人需要对其用例进行贪婪的重新验证。
【解决方案2】:

编辑:实际上,此修复程序似乎已合并到版本 1.12.0 中,您可以在此处找到它的 CDN 指针:http://jqueryvalidation.org/

供参考:

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

在找到上面的解决方案之前,我在下面找到了这段代码,所以我的建议是使用上面引用的 CDN 链接,而不是将下面的代码粘贴到您的 JS 文件中。

有一个better fix out on GitHub now(滚动到最底部),我在这里复制了它。 这不是我的工作,而且写它的 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."));

到目前为止,我已经对此进行了有限的测试,但它似乎可以正常工作,所有验证都会发生(而不是像以前那样通过任何非“require_from_group”验证),所以我很满意迄今为止。我只是在我的 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...

【讨论】:

  • 这是适合我的版本。它不会破坏其他规则,并重新验证 keyup 上的组字段。
猜你喜欢
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-28
  • 1970-01-01
相关资源
最近更新 更多