我正在使用Laravel 5.3和https://github.com/1000hz/bootstrap-validator
我正在检查Laravel中我的数据库的用户电子邮件,以检查电子邮件是否正在使用中.
Bootstrap Validator上的文档声明如果电子邮件地址正常(即不存在)则返回200,如果存在则返回4xx错误.
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