加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – Laravel响应代码和消息

发布时间:2020-12-14 19:35:11 所属栏目:大数据 来源:网络整理
导读:我正在使用Laravel 5.3和 https://github.com/1000hz/bootstrap-validator 我正在检查Laravel中我的数据库的用户电子邮件,以检查电子邮件是否正在使用中. Bootstrap Validator上的文档声明如果电子邮件地址正常(即不存在)则返回200,如果存在则返回4xx错误.
我正在使用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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读