微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – Laravel响应代码和消息

我正在使用Laravel 5.3和https://github.com/1000hz/bootstrap-validator

我正在检查Laravel中我的数据库用户电子邮件,以检查电子邮件是否正在使用中.

Bootstrap Validator上的文档声明如果电子邮件地址正常(即不存在)则返回200,如果存在则返回4xx错误.

在我的Laravel功能中,我正在执行以下操作:

public function check_email(Request $request)
{
    // Email Address
    $email = User::where('email', $request->input('email'))
            ->take(1)
            ->get();

    $email = $email->toArray();

    if(empty($email))
    {
        return response()->json(['Email Not Taken'], 200);
    }
    else
    {
        return response()->json(['Email In Use'], 400);
    }
}

现在当我运行验证器时,它没有显示消息.这是因为Laravel对其响应代码更加严格吗?或者我只是做错了什么?

我的HTML代码如下:

<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                                    <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" placeholder="Email" data-remote="/validate-email" data-error="Please enter your Email Address" required="required">
                                    @if ($errors->has('email'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('email') }}</strong>
                                         </span><!-- /.help-block -->
                                    @endif
                                    <span class="glyphicon form-control-Feedback" aria-hidden="true"></span>
                                    <div class="help-block with-errors"></div>
                            </div><!-- /.form-group -->

如何在Larvel 5.3中使用消息设置4xx响应?

谢谢

解决方法:

首先,不要通过查询检查用户是否存在使用Laravel的验证器类https://laravel.com/docs/5.3/validation它已经为您完成了这项工作,请参阅:https://laravel.com/docs/5.3/validation#rule-unique

其次,看看这个库已经完成了你需要的工作:https://github.com/proengsoft/laravel-jsvalidation

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐