dongshen2903 2014-06-17 12:06
浏览 40
已采纳

欧芹远程验证

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?

  • 写回答

2条回答 默认 最新

  • dsxpt62448 2014-06-20 08:03
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)