I'm having a hard time getting the value (live) of an input and pass it on addAsyncValidator method as a data. The addAsyncValidator always gets the value that has been set on the value attribute of an input when the page is rendered instead of the value of the input when it is changed. Here's my code:
Markup
<form action="/edit/email" method="POST" id="my_form">
<div class="row">
<div class="col-lg-4">
<div class="form-group">
<label for="email">Email</label>
<input type="text"
class="form-control"
name="email"
id="email"
{{/* value="{{.User.Email}}" */}}
value="foo@bar.com"
data-parsley-required="true"
data-parsley-type="email"
data-parsley-whitespace="trim"
data-parsley-trigger="change"
data-parsley-remote
data-parsley-remote-validator="checkEmailTaken"
data-parsley-remote-message="">
</div>
</div>
</div>
<button role="button" type="submit" class="btn btn-primary">Submit</button>
</form>
JQuery
$(function () {
$("#my_form").parsley();
$("#my_form").on("submit", function (e) {
e.preventDefault();
makeFormRequest(this); // Ajax call
});
Parsley.addAsyncValidator("checkEmailTaken", function (xhr) {
if (404 === xhr.status) {
r = $.parseJSON(xhr.responseText);
this.addError("remote", { message: r.error });
}
return 200 === xhr.status;
}, "/check/email/taken", {
"data": {
"email": $("#email").val(), // The problem here is the val is always "foo@bar.com" even when input value is changed by the user
"id": "{{.User.ObjectID.Hex}}"
}
});
});