0

我有一个控制器来验证一些输入,当验证失败时,它会将错误传递给我的视图:

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 验证错误,它显示:

验证.url

如何让 Laravel 显示在 app/lang/en/validation.php 中配置的默认、用户友好的错误消息?

因此,对于 URL 错误,这应该是:

"The :attribute format is invalid."
4

1 回答 1

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',
    ),
),
于 2013-08-06T15:27:55.153 回答