我正在尝试在Laravel中实现表单验证,但出现了问题,这很奇怪,因为通常它都能正常工作。
在我的用户模型中,我已经创建了用于验证的规则:
public static $rules = array(
'firstname'=>'required|min:2|alpha',
'lastname'=>'required|min:2|alpha',
'email'=>'required|email|unique:users',
'password'=>'required|alpha_num|between:8,12|confirmed',
'password_confirmation'=>'required|alpha_num|between:8,12',
'telephone'=>'required|between:10,12',
'admin'=>'integer'
);
在我的用户控制器中,我只有在验证通过的情况下才定义操作,否则用户将被重定向回来并显示错误信息:
public function postCreate() {
$validator = Validator::make(Input::all(), User::$rules);
if ($validator->passes()) {
$user = new User;
$user->firstname = Input::get('firstname');
$user->lastname = Input::get('lastname');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->telephone = Input::get('telephone');
$user->save();
return Redirect::to('users/signin')
->with('message', 'Thank you for creating a new account. Please sign in.');
}
return Redirect::to('users/create-account')
->with('message', 'Something went wrong')
->withErrors($validator)
->withInput();
}
以下错误已发生:
在视图中,如果存在错误,则显示它们:@if($errors->has())
<div id="form-errors">
<p>The following errors have occurred:</p>
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div><!-- end form-errors -->
@endif
我的问题是,当提交一个空表单时,它的输出结果是:
validation.required
validation.required
validation.required
validation.required
validation.required
validation.required
改为:
The firstname field is required.
The lastname field is required.
The email field is required.
The password field is required.
The password confirmation field is required.
The telephone field is required.
如果有人能够建议我为什么会出现这种情况,或者我漏掉了什么,那将不胜感激!