我有一个控制器可以验证某些输入,当验证失败时,它会将错误传递给我的视图:
public function updateAccount(){
$user = Auth::user();
$validation = User::validateAccount(Input::all());
if( $validation->passes() ){
$user->fill(Input::all());
$user->save();
return Redirect::back();
} else {
return Redirect::back()
->withErrors($validation)
->withInput();
}
}
User :: validateAccount()的代码;看起来像这样:
public static function validateAccount($input){
$rules = [
'website' => 'url'
];
$validation = Validator::make($input, $rules);
return $validation;
}
在我看来,我会显示如下错误:
@if($errors->any())
<div class="errors">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
然而,我得到了这个,而不是默认的,用户友好的错误输出。对于URL验证错误,它会显示:
validation.url
如何让Laravel显示在app / lang / en / validation.php中配置的默认用户友好错误消息?
因此,对于URL错误,这应该是:
"The :attribute format is invalid."
答案 0 :(得分:1)
更改
public static function validateAccount($input){
$rules = [
'website' => 'url'
];
$validation = Validator::make($input, $rules);
return $validation;
}
到
public static function validateAccount($input){
$messages = [ 'url' => 'You must give a valid url'];
$rules = [ 'website' => 'url' ];
$validation = Validator::make($input, $rules, $messages);
return $validation;
}
或强>
您可以将其添加到app / lang / en / validation.php文件中:
'custom' => array(
'website' => array(
'url' => 'You must give a valid url',
),
),