jquery-validation: Remote and depends bug

When you try to remote with depends, the AJAX’s parameters are not taken. The simple test case below illustrates it:

$('#field').rules('add', {
  required: true,
  remote: {
    depends: function(el) {
      return $(el).val() != loginValue;
    },
    url: 'myurl',
    type: 'POST'
  },
  messages: {
    remote: "fail"
  }
});

The call is made via GET to the page’s url, not in POST to ‘myurl’.

About this issue

  • Original URL
  • State: closed
  • Created 12 years ago
  • Comments: 16

Most upvoted comments

I also encountered this issue, it’s not a new thing as I see posts referencing the same scenario from at least 3 years ago.

I did find a very acceptable workaround, but I’d rather not use it if the underlying code is possibly going to be “fixed” which would likely break this solution. 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.

Here’s a sinippet of what my rule ends up looking like:


        rules: {
          'appointment_type[name]': {
            required: true,
            remote: {
              param: {
                url: "/hw/validate/appointment_type/name.json",
                data: {
                  'appointment_type[id]': "5"
                }
              },
              depends: function() {
                return $("#appointment_type_name").val() !== "Adjustment2";
              }
            }
          },

As far as I can tell this is a 100% solution to the issue, with the only downside being the possibility future versions of the validation plugin would change this behavior.