Reputation: 387
My back-end validation only accepts 1 parameter: email
When I look at Firebug, I can see that the URL of the request sends 2 parameters:
https://example.com/rest/checkDupEmail?newEmail=myEmail%40myEmail.com&email=
Here is the validation code...
HTML:
<input type="textbox" name="newEmail" id="newEmail"/>
JS:
validator = $('#emailForm').validate({
rules: {
newEmail: {
required: true,
remote: {
url: '/rest/checkDupEmail',
data: { email: $('#newEmail').val()},
dataFilter: function(data) {
var json = JSON.parse(data);
console.log($('#newEmail').val());
console.log(data);
}
}
}
}
});
It's like its taking the HTML field I specify (newEmail) and sending it as a parameter?
Upvotes: 4
Views: 11968
Reputation: 131
Makes the email field required, an email and does a remote request to check if the given address is already taken. In addition, the http method is set to "post" and the username is sent alongside the email address
$( "#myform" ).validate({
rules: {
email: {
required: true,
email: true,
remote: {
url: "check-email.php",
type: "post",
data: {
username: function() {
return $( "#username" ).val();
}
}
}
}
}
});
Upvotes: 0
Reputation: 41
I think, that you have to change point 2, as the example of the documentation.
Instead of
data: { email: $('#newEmail').val() },
You have to use
data: { email: function() { return $('#newEmail').val(); } },
Upvotes: 3
Reputation: 98728
Quote OP:
"It's like its taking the HTML field I specify (newEmail) and sending it as a parameter?"
Yes, of course. That's the default behavior of the remote
method... it sends the data from the field being evaluated.
It's sending two data parameters because that's how you've set it up.
It's already sending the newEmail
field's value because that's the field you've selected for remote
validation. This is the default behavior when using the remote
method.
It's also sending the same value again as email
because that's exactly how you've defined it with your data
option.
data: { email: $('#newEmail').val() },
Typically, the data
option is only used when you want to send additional data along with the default data. In this case, the value of the field being evaluated, newEmail
, is the default data.
You should remove the data
option entirely and only accept newEmail
on your back end. Even without JavaScript or the jQuery Validate plugin, newEmail
would be exactly how this data naturally comes through to the server upon submit.
Remove the data
option entirely, then either rename your markup to email
or fix the server-side code to accept newEmail
... do not send the same data twice.
Documentation: http://jqueryvalidation.org/remote-method/
Upvotes: 4