DEV Community

Cover image for Custom validation error message using Laravel blade component
Faisal Shaikh
Faisal Shaikh

Posted on • Updated on

Custom validation error message using Laravel blade component

There are multiple ways to display a custom error message for a failed validation in Laravel. We can even use the blade component to achieve this.

Just put this component below the input you want to validate. Pass the name of the input to the for attribute and your custom message to the custom-message attribute. Boom you are done!!

Usage:

<x-input-error for="foo_name" custom-message="Foo is required" />
Enter fullscreen mode Exit fullscreen mode

Component:

@props(['for', 'customMessage'])
@error($for)
<p {{ $attributes->merge(['class' => 'text-sm text-red-600 mt-1']) }}>{{ $customMessage ?? $message  }}</p>
@enderror
Enter fullscreen mode Exit fullscreen mode

Top comments (0)