How to use jQuery to remotely validate a field that depends on another field in the form?

后端 未结 2 1905
夕颜
夕颜 2020-12-08 05:06

I have a form in which I am using remote validation to check if an email address already exists in the database. However, the catch is that on this form, the user can selec

相关标签:
2条回答
  • 2020-12-08 05:20

    Found the solution to the second part of my question:

     $(".email_addr").removeData("previousValue");
    

    will remove the cache of the remote request, and allow the remote request to be triggered again, using .element().

    Thus my code is as follows:

    $("select.group_id").change(function() {
        $(".email_addr").removeData("previousValue"); //clear cache when changing group
        $("#customer_form").data('validator').element('.email_addr'); //retrigger remote call
        //my validator is stored in .data() on the form
    });
    

    Solution was found here: solution

    The first part of my question was answered originally by @Jeffery To

    All that needs to be done is to change the value of the parameter to a function, rather than just a value. Jeffery's example is copied below for future googlers:

    remote: {
      url: 'remote_script.php',
      data: {
        group_id: function () {
            return $('select.group_id').val();
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-08 05:24

    From the second example for remote it looks like functions (evaluated during validation) can be used for data, so

    remote: {
        url: 'remote_script.php',
        data: {
            group_id: function () {
                return $('select.group_id').val();
            }
        }
    }
    

    should work.

    For your second question, did you try passing the validation rules to validate()?

    0 讨论(0)
提交回复
热议问题