0

Ok, this works fine:

$( "#myform" ).validate({
  rules: {
    email: {
      remote: {
        url: "/ajax/check-email.php",
        mothod: "get",
        dataFilter: function( response ) {
          return response;
        }
      }
    }
  }
});

this configuration automatically made a GET to url:

/ajax/check-email.php?email=EMAIL-TO-CHECK

The problem is that I have this API to query:

/ajax/email/EMAIL-TO-CHECK/exists

So I have to create the url to be queried at runtime, but I can't do it in any way.

I tried to do this, using $.ajax() directly:

remote: function( ele ) {

  var email = $(ele).val()

  $.ajax({
    url: "/ajax/email/"+ email + "/exists",
  }).done(function( xhr ) {
    return xhr
  });
}

ut the validation does not fire.

I also tried to use a function for get "url", but "email" is empy. Like this:

var createUrl = function() {
  var id = $("#myform input[name=email]").val(); //this is empty
  return "/manager/ajax/pricelist/" + id + "/exists"
}

$( "#myform" ).validate({
  rules: {
    email: {
      remote: {
        url: createUrl(),
      }
    }
  }
});

Any idea? Many many thanks.

1 Answer 1

2

No, this isn't possible with the existing remote: method. From the documentation:

The serverside resource is called via jQuery.ajax (XMLHttpRequest) and gets a key/value pair corresponding to the name of the validated element and its value as a GET parameter.

So this use of ?email=EMAIL-TO-CHECK is built into the method, there's no opportunity to customize of.

The simplest solution for you would probably be to create a rewrite rule on your server that rewrites something like

/ajax/check?NAME=VALUE

to

/ajax/NAME/VALUE/exists

A more complex solution is to write your own validation method that's like remote: but allows you to provide a callback function to generate the URL.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you man, you have been very clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.