admin管理员组文章数量:1415672
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 badges2 Answers
Reset to default 4 +100By 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");
}
}
}
本文标签: javascriptParsley remote validateStack Overflow
版权声明:本文标题:javascript - Parsley remote validate - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745225763a2648600.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论