简体   繁体   English

错误的jQuery验证插件取决于。 字段无效时表格有效

[英]Error jquery validation plugin depends. Form valid when fields are not valid

Problem description 问题描述

I have a web form wizard for which I need validation that supports (a) hidden fields and (b) conditional validation. 我有一个Web表单向导,我需要对其进行验证,该向导支持(a)隐藏字段和(b)条件验证。 Jquery seems the best fit. jQuery似乎最合适。 Once all is set up as follows ( simplified example ): 一切设置如下( 简化示例 ):

$( "form[name=form]" ).validate({
    ...
    ignore: [],
    rules: {
        'form[owner]': {
            required: true,
        },
        'form[bank_account]': {
            required: true, 
            depends: function(element){
                    return $('#my_select').is(':checked');`
    messages : {
        'form[owner]' : {
             required: "The owner is necessary",
        },
        'form[bank_account]' : {
            required: "This field must be filled out when switch on",
         },
    ...
})

Error description 错误说明

  1. I force the validation to go wrong by submitting the form without filling out the fields listed above. 我通过提交表单而不填写上面列出的字段来强制验证出错。
  2. In my case, as there is a wizard, I output messages with an alert, for the errors are not visible at first. 就我而言,由于有一个向导,我输出带有警报的消息,因为这些错误一开始是不可见的。
  3. I go back in the form, fill out just owner , and try to force again the validation to fail by submitting, with my_select checked 我返回表格, 仅填写owner ,然后尝试通过提交,并选中my_select再次强制验证失败
  4. The validation is passed (and form submitted) although there is still a field which is not valid, resulting in an error on the server-side. 尽管仍有一个无效字段,但通过了验证(并提交了表单),从而导致服务器端发生错误。

On javascript console, I got the following error: 在javascript控制台上,出现以下错误:

Cannot read property 'call' of undefined. 无法读取未定义的属性“ call”。 Exception occurred when checking element form[bank_account], check the method depends 检查元素表单[bank_account]时发生异常,检查方法取决于

Expected result 预期结果

All fields should be validated accordingly to form validation each and every time the form is to be submitted 每次提交表单时,都应相应地验证所有字段以进行表单验证

How can this be fixed? 如何解决?

As I did not find any questions similar to mine around. 由于我没有发现任何类似的问题。 The solution I found is the following: 我找到的解决方案如下:

Instead of using the depends method, use a function to establish if the field should or should not be required, including it in the conditions on which this operation should rely (ie if a checkbox is checked or not) 可以使用函数来确定是否需要该字段,而不是使用depends方法,包括在此操作应依赖的条件下(例如,是否选中复选框)

$( "form[name=form]" ).validate({
    ...
    ignore: [],
    rules: {
        'form[owner]': {
            required: true,
        },
        'form[bank_account]': {
            required: function(element){
                    return $('#my_select').is(':checked') && $('form_bank_account').val();
    messages : {
        'form[owner]' : {
             required: "The owner is necessary",
        },
        'form[bank_account]' : {
            required: "This field must be filled out when switch on",
         },
    ...
})

Either documentation is confusing, or the library has a bug. 文档令人困惑,或者库有错误。

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

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