0

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."
2
  • Can you provide your code for User::validateAccount(); please Commented Aug 6, 2013 at 15:17
  • Added the code for User::validateAccount(); to my original post. Commented Aug 6, 2013 at 15:26

1 Answer 1

1

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',
    ),
),
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.