.VALIDATE : CANCEL THE VALIDATION FOR A FIELD BASED ON REMOTE RESPONSE

我们两清 提交于 2020-01-05 12:30:06

问题


Consider the following code:

$( 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();
    }
});
});

I want to be able to cancel the validation for the field "reportingto" if the response back from the AJAX call is false.

Any ideas/help gratefully received..!

Regards

Oli

THANKS FOR THE RESPONSES SO FAR BUT I STILL HAVE ISSUES.

I tried using depends but still doesn't work as required:

Take the following "cut down" test code

        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
        },  

and the field

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

If I force a "false" return from the AJAX it still triggers a validation error message for the field.


回答1:


Use depends Parameter.

$( 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();
    }
});

});

The workaround I found is to nest the standard remote: properties under a property named param:. If the depends: function returns true, it will reference the properties as you would expect.

For documentation see HERE

The above Code should work , U have alternative also ..

    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 reporting element.



来源:https://stackoverflow.com/questions/24891764/validate-cancel-the-validation-for-a-field-based-on-remote-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!