Marc Rasmussen Marc Rasmussen - 6 months ago 72
Javascript Question

Parsley remote validate

I have the following input field for username:

<input type="email" class="form-control" id="txt_username" name="username" data-parsley-trigger="change" data-parsley-remote="/User/user_exists" data-parsley-remote-options='{ "type": "POST", "dataType": "json", "data": { "request": "ajax" } }'>

This works fine and calls my
PhP function

public function user_exists()
$user = $this->getDatabase()->prepTemplate('SELECT * FROM User WHERE username = ? ', 's', array($_POST['username']), MySqlTemplates::RFQ_FM);
if($user != null)
print json_encode("400");
print json_encode("200");

However i am unsure what to do to either deny or allow the validation.

the documentation isnt much of help (atleast i have trouble finding it)

Can anyone give me a push in the right direction?


By default, parsley.remote will consider all 2xx ajax responses as a valid response, and all the others as wrong response.

We have the same concern in our app, leveraging Parsley to tell a user if the username / email he wants is available in our database. To do so, and keep a correct REST API response (200 if user found, 404 if not), you need to tell parsley.remote to do the opposite of this behavior either by:

  • using data-parsley-remote-reverse="true"
  • using data-parsley-remote-validator="reverse" to tell to use the reverse validator (exact same thing as above)
  • last but not least, create your own validator (that we did in our project) for this check:

    window.ParsleyExtend.asyncValidators['remote-email'] = function (xhr) { return xhr.status === 404; };

and use data-parsley-remote-validator="remote-email"

All that is explained here in the doc.

Hope that helped.