How to customize or Change validation error messages

Answers 2
  • Validation error messages customization in laravel

    //Step 1. Create form request using CLI
    php artisan make:request StorePostRequest
    
    //Step 2. Add validation rules to rules() method and custom messages in messages() method in created file</strong></p>
    // app\Http\Requests\StorePostRequest.php
    public function authorize()
    {
        return true;
    }
    public function rules()
    {
        return [
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
        ];
    }
    
    public function messages(){
        return [
            'title.required' => 'Post title is required',
            'body.required' => 'Post Body is required',
        ];
    }
    
    //Step 3. Make changes in your controller file
    // app\Http\Controllers\<YourController>.php
    use App\Http\Requests\StorePostRequest;
    
    public function store(StorePostRequest $request)
    {
        $validated = $request->validated();
        dd($validated);
    }
    
    //Step 4. Display validation errors in view file
    <!-- resources\views\<yourfile>.blade.php -->
    
    @if($errors->any())
    <div class="alert alert-danger" role="alert">
        <ul class="list-unstyled">
            @foreach($errors->all() as $error)
            <li> {{ $error }} </li>
            @endforeach
        </ul>
    </div>
    @endif
    
    0

    You can customize or change the validation error messages used by the form request by changing the array of attribute / rule pairs ('title.required') with the custom error message in messages() method Like for required title attribute you can use  'title.required' => 'Post title is required'.

  • Custom validation error messages using validate() or Validator::make() method

    //Method 1. Custom validation error message using Validator facade.
    //Before class definition
    use Illuminate\Support\Facades\Validator;
    
    // Inside controller’s method
    $validator = Validator::make($request->all(),  [
        'title' => 'required',
        'body' => 'required',
    ],
    [
        'required' => 'The :attribute field can not be blank.',
        'body.required' => 'Custom validation message for body attribute on required.',
    ]);
    
    if($validator->fails()){
        return  redirect()->back()->withErrors($validator)->withInput();
    }
    
    //Method 2. Custom validation error message using validate() method.
    // inside controller's method -  app\Http\Controllers\<YourController>.php
    $this->validate($request, [
        'title' => 'required',
        'body' => 'required',
    ],
    [
        'required' => 'The :attribute field can not be blank.',
        'body.required' => 'Custom validation message for body attribute on required.',
    ]);
    
    0

    We can easily add custom validation error messages by passing a third argument to the validate() or Validator::make() method containing an array of our custom validation error messages. Basically, these methods accept four parameters, form request data, validation rules, custom error messages, and custom attributes.

  • Back to code snippet queries related laravel