.VALIDATE:取消基于远程响应的现场验证

时间:2014-07-22 15:38:26

标签: jquery validation jquery-validate

请考虑以下代码:

$( 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调用返回的响应是错误的。

任何想法/帮助都感激不尽......!

此致

奥利

感谢我们的反响,但我还有问题。

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

采取以下措施"减少"测试代码

        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>

如果我强迫&#34;假&#34;从AJAX返回它仍会触发该字段的验证错误消息。

1 个答案:

答案 0 :(得分:0)

使用取决于参数。

$( 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报告元素。