Custom Validation Message Laravel

  • 26-09-2022
  • 1142
  • Laravel 9
  • Haresh Chauhan

Custom Validation Message Laravel Example; laravel provided a default validation message for each validation rule. The laravel validation message is very easy to read and there is no need for any problem with it. But you sometimes want to change your custom message instead of the laravel default validation message. we will see in this example.

Laravel provided a custom add validation error message modification system also in the application. For that, you need to pass an array of the defined error message for specific field validation naming. This is very easy to learn.

When you add your custom validation message the laravel default validation replace with yours or overwrite the default message with your message.

So let's see a custom validation error message with some examples.

Example 1. Simple In Controller

In this Example, We will pass two arrays for validating the form request. The first array for the field rules. and the second array the for the rename the field value from the existing default laravel message. If you have more than two validation on the single field also you need to define the same as in the request message added. You can add each validation rule for the same field.

public function store(Request $request)
{
    request()->validate([
        'file' => 'required|mimes:png',
        'type' => 'required|max:150'
    ],
    [
        'file.required' => 'You have to choose the file!',
        'file.mimes' => 'The file must be png format!',

        'type.required' => 'You have to choose type of the file!'
        'type.type' => 'The type must be less than 150 characters!'
    ]);
}

Example 2. Request Class

How To Add After Validation Hook In Laravel Example

In this example for the request class, There by default in the request class not defined messages() method while you creating. You need to put your self manually messages() method. See the below example.

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class TestRequest extends FormRequest
{
    /**
    * Determine if the user is authorized to make this request.
    *
    * @return bool
    */
    public function authorize()
    {
        return true;
    }

    /**
    * Get the validation rules that apply to the request.
    *
    * @return array
    */
    public function rules()
    {
        return [
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
            'author.name' => 'required',
        ];
    }

    public function messages()
    {
        return [
            'title.reuired' => 'A nice title is required for the post.',
            'title.max' => 'The title should be at least 255 character',

            'body.required' => 'Please add content for the post.',
        ];
    }
}

Example 3. API & Array Method.

In this example, you may use it while you making an AJAX request or an API post request handle. We will create two array variables in the first $rules variable we will pass all our validation rules for each form request field, While in the $message variable we will pass our custom-made massage. After it passes it on to the validator rule maker class. The message or the third argument is not compulsory.

public function store(Request $request)
{
    $rules = [
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
        'author.name' => 'required',
    ];

    $message = [
        'title.reuired' => 'A nice title is required for the post.',
        'title.max' => 'The title should be at least 255 character',

        'body.required' => 'Please add content for the post.',
    ];

    $validator = \Validator::make($request->all(),$rules,$message);

    if ($validator->fails())
    {
        $validator->errors(); // DO YOUR ACTION WITH VALIDATION MESSAGE
    }
}

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :