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