gpt4 book ai didi

javascript - 欧芹远程验证

转载 作者:行者123 更新时间:2023-11-30 08:43:19 25 4
gpt4 key购买 nike

我有以下用户名输入字段:

<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" } }'>

这工作正常并调用我的 PhP 函数:

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");
}
}
}

但是我不确定如何拒绝或允许验证。

文档没有多大帮助(至少我很难找到它)

任何人都可以在正确的方向上插入我吗?

最佳答案

默认情况下,parsley.remote 会将所有 2xx ajax 响应视为有效响应,而将所有其他响应视为错误响应。

我们在我们的应用程序中也有同样的担忧,利用 Parsley 告诉用户他想要的用户名/电子邮件是否在我们的数据库中可用。为此,并保持正确的 REST API 响应(如果用户找到则为 200,如果未找到则为 404),您需要通过以下方式告诉 parsley.remote 做相反的事情:

  • 使用 data-parsley-remote-reverse="true"
  • 使用 data-parsley-remote-validator="reverse" 告诉使用反向验证器(与上面完全相同)
  • 最后但同样重要的是,为此检查创建您自己的验证器(我们在项目中所做的):

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

并使用data-parsley-remote-validator="remote-email"

所有的解释here在文档中。

希望对您有所帮助。

最佳

关于javascript - 欧芹远程验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24263380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com