How to change default validation messages in Laravel

Categorized as Laravel Tagged

Sometimes you need to change the default text of validation error messages returned when data validation fails. Fortunately, with form requests, you can make our own error messages to make it clearer to the user.

// When returning error messages for failed data validation, the default messages 
// are not always easy for user to understand. However, if you're using
// form requests, you can supply your own texts via the 'messages' method. You can 
// even provide errors for individual rules if you require multiple conditions:

use Illuminate\Foundation\Http\FormRequest;

class StoreRequest extends FormRequest
{
  public function rules()
  {
    return [
      'address' => 'required|email',
    ];
  }

  public function messages()
  {
    return [
      'address.required' => 'You need to supply an email address',
      'address.email' => 'This does not look like a valid email address',
    ];
  }

}

Leave a reply

Your email address will not be published.