我已经在我的控制器中写了一些登录形式的验证。
$validator = Validator::make(Input::all(), [
'email' => 'email|required',
'password' => 'required'
]);
// If validation fails
if($validator->fails()) {
// Redirect to some place
return Redirect::route('account-sign-in-get')->withErrors($validator)->withInput();
} else {
// Attempt to sign in
}
在我的观点中,我有以下内容:
@if($errors->has('email'))
<div class="alert alert-warning" role="alert">
{{ $errors->first('email') }}
</div>
@endif
我输入一个字符串(不是有效的电子邮件地址),然后当我提交时,我预计会出现错误,指出:&#34;电子邮件必须是有效的电子邮件地址&#34;
相反,我得到:&#34;电子邮件字段是必需的&#34;
{{ Form::open(['route' => 'account-sign-in-post', 'role' => 'form']) }}
<div class="form-group">
{{ Form::label('email', 'E-mail', ['for' => 'email', 'class' => 'sr-only ']) }}
{{ Form::text('username', NULL, ['placeholder' => 'Email', 'id' => 'email', 'class' => 'form-control']) }}
@if($errors->has('email'))
<div class="alert alert-warning" role="alert">{{ $errors->first('email') }}</div>
@endif
</div><!-- End of .form-group -->
<div class="form-group">
{{ Form::label('password', 'Password', ['for' => 'password', 'class' => 'sr-only']) }}
{{ Form::password('password', ['placeholder' => 'Password', 'class' => 'form-control', 'id' => 'password']) }}
@if($errors->has('password'))
<div class="alert alert-warning" role="alert">{{ $errors->first('password') }}</div>
@endif
</div><!-- End of .form-group -->
{{ Form::submit('Sign in', ['class' => 'btn btn-primary btn-block']) }}
{{ Form::close() }}
感谢您阅读本文。任何帮助表示赞赏:)
答案 0 :(得分:5)
错误在于形式。
特别是我们有一个用户名的文本输入。
{{ Form::text('username', NULL, ['placeholder' => 'Email', 'id' => 'email', 'class' => 'form-control']) }}
应该是电子邮件:
{{ Form::text('email', NULL, ['placeholder' => 'Email', 'id' => 'email', 'class' => 'form-control']) }}
答案 1 :(得分:1)
@arkhamDev是对的,首先你需要添加这些更改,而不是遵循以下规则。
堕落规则的顺序在这里很重要。 你有以下
$validator = Validator::make(Input::all(), [
'email' => 'email|required',
'password' => 'required'
]);
请尝试以下代码
$validator = Validator::make(Input::all(), [
'email' => 'required|email',
'password' => 'required'
]);