Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jordantsap's avatar

How to show validation rules in blade input

Not the errors but the validation rules so that user can know beforehand what the rule for specific input is. E.g when the rule for password field is min:8, to show something like "Please input minimum of 8 chars" in the input placeholderr or somewhere around the field

0 likes
3 replies
LaryAI's avatar
Level 58

One way to achieve this is by using the attributes() method in your form request class.

In your form request class, you can override the attributes() method and return an array of custom attribute names and their corresponding validation rules.

Here's an example:

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class YourFormRequest extends FormRequest
{
    public function attributes()
    {
        return [
            'password' => 'Please input minimum of 8 chars',
        ];
    }

    // rest of your form request code...
}

Then, in your blade template, you can access the custom attribute names using the getAttribute() method on the form request instance.

Here's an example of how you can display the custom attribute name as a placeholder in an input field:

<input type="text" name="password" placeholder="{{ $request->getAttribute('password') }}">

Make sure to replace YourFormRequest with the actual name of your form request class.

This way, you can define custom attribute names for each input field and display them as placeholders or anywhere else you need in your blade templates.

1 like
MohamedTammam's avatar

Laravel doesn't have a way to know that in blade before user submission. You should write it yourself.

Or you mean the meaningful error message?

Should display the error message.

@error('password')
	{{ $message }}
@enderrors

For displaying all errors

@if($errors->any())
	@foreach($errors->all() as $e)
		{{ $e }}
	@endforeach
@endif

https://laravel.com/docs/10.x/blade#validation-errors

Snapey's avatar

I find it easiest to just type the requirements next to the field

1 like

Please or to participate in this conversation.