Laravel Form Validation Custom Error Messages Example

Today, i will let you know that how to show custom form validation error message in laravel blade. If you give non validate data before submit form then laravel automacally shows their default error message. You will also learn how to show error message in laravel blade from this tutorial.

But in this tutorial we will show our own custom error mesaage. Do you know how to show custom validation error message in Laravel? If you don't know that this tutorial is for you. If laravel custom validation message not working for you then you can follow this example.

Just paste this code in your controller method.

public function submit_customer_data(Request $request)
    {
      $rules = [
          'name' => 'required|min:6',
          'email' => 'required|email|unique:customers'
      ];

      $errorMessage = [
          'required' => 'Enter your :attribute first.'
      ];

      $this->validate($request, $rules, $errorMessage);
      
      //save data here

      session()->flash('message','Customer created successfully!');
      return redirect()->back();

    }

 

You can also use the below system to show different error message for different field.

public function store()
{
    request()->validate([
        'file' => 'required',
        'type' => 'required'
    ],
    [
        'file.required' => 'You have to choose the file!',
        'type.required' => 'You have to choose type of the file!'
    ]);
}

 

And after doin that paste this below code to your blade file.

 

Read also : Laravel 7.x Form Validation with Custom Error Messages

Now you can check it. Hope it can help you.