【问题标题】:.VALIDATE : CANCEL THE VALIDATION FOR A FIELD BASED ON REMOTE RESPONSE.VALIDATE : 取消对基于远程响应的字段的验证
【发布时间】:2014-07-22 15:38:26
【问题描述】:

考虑以下代码:

$( document ).ready(function() {
$("#register_employee_form").validate({
    errorElement: "p",      
    rules: 
    {
        forename: "required",
        surname: "required",
        reportingto: 
        {
            required : true,
            remote: {
                url: "ajax/error_checking/check_reporting_to.php",
                type: "post",
                data: {
                    reportingto: function() {
                        return $( "#reportingto" ).val();   // pass reportingto field
                    }
                },
                complete: function(response) {
                    if (response.responseText == "false")
                    {
                        // CANCEL THE VALIDATION FOR THIS FIELD
                        // HOW DO I DO IT??????
                    }
                }
            }
        }
    },
    messages: {
        forename: "Please enter your first name",
        surname: "Please enter your last name",
        reportingto: "Employee required"

    },
    submitHandler: function(form) {
        form.submit();
    }
});
});

如果从 AJAX 调用返回的响应为假,我希望能够取消对“reportingto”字段的验证。

感激地收到任何想法/帮助..!

问候

奥利

感谢到目前为止的回复,但我仍有问题。

我尝试使用依赖,但仍然无法按要求工作:

采取以下“删减”测试代码

        test_field: 
        {
            required: {
                remote: {

                  param: {

                    url: "ajax/error_checking/check_reporting_to.php",
                    type: "post",
                    data: {
                        reporting_to: function() {
                            return $( "#test_field_to" ).val();   // pass reporting_to field
                        }
                    },
                    complete: function(response) {
                        if (response.responseText == "false")
                        {
                            cancel_flag = 1;
                        }
                    }

                  } // param
                  ,
                 depends: function() {
                     if(cancel_flag == 1)
                     {
                         return false;
                     }
                     else 
                     {
                         return true;
                     }
                  }

                } // remote
            } // required
        },  

和领域

 <td class="column_1"><label>TEST FIELD</label></td>                    
 <td><input name="test_field" id="test_field" value=""  /></td>

如果我强制从 AJAX 返回“假”,它仍然会触发该字段的验证错误消息。

【问题讨论】:

  • rules: { address: { required: { depends: function(element) { if ($('#newsletterByPost').is(':checked')){ return true; }else{ return false; } } } } }
  • 您可以咨询贵公司的高级 PHP/javasript 开发人员吗?
  • 恐怕只有我一个人。我一个人工作。离开了几个星期。将再看看它。它应该工作,所以我不明白为什么它不工作。我将尝试一个新示例,将所有内容立即剥离,以确保没有其他任何影响。

标签: jquery validation jquery-validate


【解决方案1】:

使用依赖参数。

$( document ).ready(function() {
var cancelFlag=0; //Dont cancel validation

$("#register_employee_form").validate({
    errorElement: "p",      
    rules: 
    {
        forename:{required : true},//Do u assure on this LINE CODE BY U WORK ? I made it Proper , If ur Old code works then no problem
        surname:{required : true},//Do u assure on this LINE CODE BY U WORK ? I made it Proper , If ur Old code works then no problem
        reportingto: 
        {                       
            required : true,
            remote: {

              param: {

                url: "ajax/error_checking/check_reporting_to.php",
                type: "post",
                data: {
                    reportingto: function() {
                        return $( "#reportingto" ).val();   // pass reportingto field
                    }
                },
                complete: function(response) {
                    if (response.responseText == "false")
                    {
                        cancelFlag = 1;
                        // CANCEL THE VALIDATION FOR THIS FIELD
                        // HOW DO I DO IT??????
                    }
                }

              }//Param
              ,
             depends: function() {
                 if(cancelFlag==1)
                 {
                     return false;
                 }
                 else 
                 {
                     return true;
                 }
              }

            }//Remote


        }
    },
    messages: {
        forename: "Please enter your first name",
        surname: "Please enter your last name",
        reportingto: "Employee required"

    },
    submitHandler: function(form) {
        form.submit();
    }
});

});

我发现的解决方法是将标准 remote: 属性嵌套在名为 param: 的属性下。如果depends: 函数返回true,它将像您期望的那样引用属性。

有关文档,请参阅HERE

上面的代码应该可以工作,你也有其他选择..

    reportingto: 
    {                       
        required : {
            remote: {

          param: {

            url: "ajax/error_checking/check_reporting_to.php",
            type: "post",
            data: {
                reportingto: function() {
                    return $( "#reportingto" ).val();   // pass reportingto field
                }
            },
            complete: function(response) {
                if (response.responseText == "false")
                {
                    cancelFlag = 1;
                    // CANCEL THE VALIDATION FOR THIS FIELD
                    // HOW DO I DO IT??????
                }
            }

          }//Param
          ,
         depends: function() {
             if(cancelFlag==1)
             {
                 return false;
             }
             else 
             {
                 return true;
             }
          } 

        }//Remote
        }
    }

FOR 报告元素。

【讨论】:

  • 感谢您的友好回答,他们提供了一些帮助,但我仍然无法让它按照我的意愿工作。查看问题的补充
  • 由于某种原因,上述解决方案似乎没有触发 AJAX 调用。仍在为此苦苦挣扎。
  • @OliverTsang ,你从上个月开始为这个问题苦苦挣扎吗?
猜你喜欢
  • 2015-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多