Reputation: 145
Good Evening,
We have a from with multiple fields.
We use parsley for validation. All the fields are validated using locally using Parsley, except the email address which makes a trip to the server. To accomplish this, we are using the .addValidator method.
Our custom code which adds the custom validator is as below
window.ParsleyValidator
.addValidator('checkemail', {
validateString: function(value)
{
return $.ajax({
url:'ajax/DoesUserExist.php',
method: "POST",
data: {email:value},
dataType: "json",
success: function(data)
{
return true;
}
})
}
})
Our html field looks like this.
<input name="signup-emailAddress" id="signup-emailAddress" type="email" class="form-control"
data-parsley-required="true"
data-parsley-checkemail
data-parsley-checkemail-message="Email address already exists."
data-parsley-type="email" data-parsley-trigger="focusout" />
The ajax call works beautifully and we get the results we want visually. But then when we go to make a final validation with
if ($('#form-sign-up').parsley().validate() )
we get some strange results. If there are errors anywhere on the page, the result comes back as "false" as it should. But when we validate any with no error message, it comes back as null.
I have attached the console.log from chrome
That makes it very hard to figure out when to actually save the record. The error only occurs on the email field since that is the only field that I have this custom validator attached to.
Any help would really be appreciated.
All the best, George Eivaz
Upvotes: 0
Views: 956
Reputation: 145
The only way I was able to make this call work is to add async:false to the $.ajax call. The issue that became obvious in debugging this made it clear that because the call is asynchronous, by the time the database comes back with a response, it is too late for parsley.
Upvotes: 0