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

luisferfranco's avatar

How do I use the #[Validate()] attribute in Livewire 3 for unique validation?

Inside my update() function I have this validation rule that works:

// ...
'email' => [
  'required',
  'email',
  Rule::unique('users')->ignore($this->user),
],
// ...

But I'd like to use the #[Validate()] attribute, since I think its cleaner and more modern. Another reason is that I can create custom messages easily (I'm not a native English speaker, so it is super useful to create custom messages)

Problem is that if I use something like:

#[Validate('unique:users,email')]
public $email;

It tells me I need a rules() function. If I need a rules() function then what's the point on having the attribute. I like the attribute syntax specially because of the easyness of declaring the error messages to be displayed

Is it possible, or for this case its mandatory either to do the validation in a method, or using a rules() function?

0 likes
1 reply
s4muel's avatar

make sure you have imported the correct Validate class use Livewire\Attributes\Validate; this should work then:

#[Validate('unique:users,email')]
public $email;

but bear in mind, you cant put more complex rules in there (like ignoring the current user in the unique rule). from the documentation: https://livewire.laravel.com/docs/validation#validate-attributes

Validate attributes don't support Rule objects PHP Attributes are restricted to certain syntaxes like plain strings and arrays. If you find yourself wanting to use run-time syntaxes like Laravel's Rule objects (Rule::exists(...)) you should instead define a rules() method in your component.

Please or to participate in this conversation.