0
votes

I have a controller that validates some input and when validation fails it passes the errors to my view:

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();

    }
}

The code for User::validateAccount(); looks like this:

public static function validateAccount($input){
    $rules = [
        'website' => 'url'
    ];
    $validation = Validator::make($input, $rules);
    return $validation;
}

In my view I display the errors like this:

@if($errors->any())
  <div class="errors">
      <ul>
          @foreach($errors->all() as $error)
                <li>{{ $error }}</li>
          @endforeach
      </ul>
  </div>
@endif

However, instead of the default, user friendly error output I get this. For a URL validation error it displays:

validation.url

How do I get Laravel to display the default, user friendly error messages that are configured in app/lang/en/validation.php?

So for a URL error this should be:

"The :attribute format is invalid."
1
Can you provide your code for User::validateAccount(); pleaseGareth Oakley
Added the code for User::validateAccount(); to my original post.Thomas Teerlink

1 Answers

1
votes

You need to define the custom message.

Change

public static function validateAccount($input){
    $rules = [
        'website' => 'url'
    ];
    $validation = Validator::make($input, $rules);
    return $validation;
}

to

public static function validateAccount($input){
    $messages = [ 'url' => 'You must give a valid url'];
    $rules = [ 'website' => 'url' ];
    $validation = Validator::make($input, $rules, $messages);
    return $validation;
}

OR

You can add this to your app/lang/en/validation.php file:

'custom' => array(
    'website' => array(
        'url' => 'You must give a valid url',
    ),
),