最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Parsley remote validate - Stack Overflow

programmeradmin0浏览0评论

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()
{
    if($this->isAjax())
    {
        $user = $this->getDatabase()->prepTemplate('SELECT * FROM User WHERE username = ? ', 's', array($_POST['username']), MySqlTemplates::RFQ_FM);
        if($user != null)
        {
            print json_encode("400");
        }
        else
        {
            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?

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()
{
    if($this->isAjax())
    {
        $user = $this->getDatabase()->prepTemplate('SELECT * FROM User WHERE username = ? ', 's', array($_POST['username']), MySqlTemplates::RFQ_FM);
        if($user != null)
        {
            print json_encode("400");
        }
        else
        {
            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?

Share Improve this question edited Feb 19, 2019 at 7:10 Shahar Shokrani 8,79210 gold badges57 silver badges102 bronze badges asked Jun 17, 2014 at 12:06 Marc RasmussenMarc Rasmussen 20.6k83 gold badges223 silver badges384 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4 +100

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.

Best

In your php file:

public function user_exists()
{
    if($this->isAjax())
    {
        $user = $this->getDatabase()->prepTemplate('SELECT * FROM User WHERE username = ? ', 's',array($_POST['username']), MySqlTemplates::RFQ_FM);
        if($user != null)
        {
            header("HTTP/1.0 404 Not Found");
        }
        else
        {
            header("HTTP/1.1 200 Ok");
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论