Forms laravel5.4验证无法显示$errors
我使用验证来确认登记表。 `$message=array('required'=>'您不能') 双向都不能得到$errors。 我可以肯定这个验证是有效的。抛出一个异常,因为在验证方法之后,所有代码都不是借口。 但我可以得到$errors,可能是直接错误的url? 这包括在注册网站中。 `Forms laravel5.4验证无法显示$errors,forms,laravel,validation,Forms,Laravel,Validation,我使用验证来确认登记表。 `$message=array('required'=>'您不能') 双向都不能得到$errors。 我可以肯定这个验证是有效的。抛出一个异常,因为在验证方法之后,所有代码都不是借口。 但我可以得到$errors,可能是直接错误的url? 这包括在注册网站中。 ` 试试这个。以这种方式设置规则和消息请共享您提交的表单的HTML。@haakym,请参阅更新。您是否使用ajax提交表单,因为我可以在表单HTML代码中看到这一点:onsubmit=“return postFo
试试这个。以这种方式设置规则和消息请共享您提交的表单的HTML。@haakym,请参阅更新。您是否使用ajax提交表单,因为我可以在表单HTML代码中看到这一点:
onsubmit=“return postForm()”
@haakym。我不使用ajax submit。我不想将真实密码发布到服务器,需要在md5之后发布,因此我重新编写了一个提交函数。请参阅我的更新。@haakym。最后,我找到了原因。我的控制器收到表单post时,控制器将请求另一台服务器并返回url,然后,控制器重定向到此url,如果我不请求另一台服务器,我可以显示错误。在刀片页面中添加错误类。我在回答中加了一页。通过它。这可能对你有帮助
$datavalidate = array(
'phone' =>$request->input('phone')
);
$rules = array(
'phone' =>'required'
);
$validator = Validator::make($datavalidate,$rules,$message);
if($validator->fails())
{
return Redirect::back()->withErrors($validator);
}
$this->validate($request,[
'phone' => 'required',
'confirmCode' => 'required'
]);`
<ul>
<li>hello</li>
@foreach($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
{{var_dump($errors)}}
<form class="form-horizontal" action="{{route('confirmRegister')}}"method="post" onsubmit="return postForm()">
{{csrf_field()}}
<div class="form-group">
<label for="mobileNum" class="col-xs-3 control-label text-center">手机号</label>
<div class="col-xs-8">
<input type="text" name="phone" class="form-control" id="mobileNum" placeholder="手机号码">
</div>
</div>
<div class="form-group">
<label for="passWord" class="col-xs-3 text-center">密码</label>
<div class="col-xs-8">
<input type="password" class="form-control" id="passWord" placeholder="密码">
<input type="hidden" id="sendPassWord" name="passWord">
<input type="hidden" id="openId" name="openId">
</div>
</div>
<div class="form-group">
<label for="confirmPassWord" class="col-xs-3 text-center">确认密码</label>
<div class="col-xs-8">
<input type="password" name="confirmPassWord" class="form-control" id="confirmPassWord" placeholder="确认密码">
</div>
</div>
<div class="form-group">
<label for="confirm" class="col-xs-3 text-center">验证码</label>
<div class="col-xs-5">
<input type="text" name="confirmCode" class="form-control" id="confirm" placeholder="短信验证码">
</div>
<div class="col-xs-4">
<button type="button" class="btn btn-info btn-block" id="getConfirm">获取验证码</button>
</div>
</div>
<div class="alert alert-danger alert-dismissible col-xs-offset-3 col-xs-8" role="alert" style="display: none" id="codeError">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>错误!</strong> 获取验证码失败!
</div>
<div class="form-group">
<div class="col-xs-offset-3 col-xs-8">
<button type="submit" class="btn btn-primary btn-block" id="register">注册</button>
</div>
</div>
</form>
function postForm() {
var passWord = md5( $('#passWord').val() );
$('#sendPassWord').val(passWord);
$('#openId').val("{{$openId}}");
return true;
}
phone<input type="text" name="phone" >
<span class="error">{{$errors->first('phone')}}</span>
confirmCode<input type="text" name="confirmCode">
<span class="error">{{$errors->first('confirmCode')}}</span>
$rules = ['phone' => 'required',
'confirmCode' => 'required',
];
$message = [
'phone.required' => 'enter your phone number',
'confirmCode.required' => 'confirm your code
];
$validator = validator::make($request->input(), $rules, $message);
if ($validator->fails()) {
return back()
->WithErrors($validator)
->WithInput();
}else{ write your codes here