Unique validation rule not applicable to db index
in my migration file i have applied a unique constraint to the email column of my user model. when trying to addionally apply a frontend validation in the user resource using the unique function, i get no result. thus i am still able to insert duplicat emails resulting in an sql error.
12 Replies
TextInput::make('email')
->required()
->unique(User::class, 'email', ignoreRecord: true)
or
TextInput::make('email')
->required()
->rule(\Illuminate\Validation\Rule::unique('users', 'email')->ignore($record->id))
both suggested solutions have no effect. there is a syntax error in the second one
It might work, however make sure to apply the corresponding field i.e
email
instead of slug

Because of your version
TextInput::make('email')
->required()
->unique(
User::class, // Model class
'email', // Column
ignoreRecord: true // <-- this tells Filament to automatically ignore current record ID
);
neither of the suggested solutions have any effect. since this error also occurs when creating new records, i don't think the ignoreRecord affects the described situation
Maybe try creating your own rule like this:
If that doesn't work, maybe try checking elsewhere for mistakes...
Is this the standard panel page or a custom page?
Standard panel, particularly in a resource
i am trying to fix this issue for several weeks now. unfortunately i can't share the repository due to a nda
So is the record in db soft deleted?
maybe you have some condition that respect the soft deleted record which may pass the
unqiue
validation and goes to dbMaybe use Debugbar or similar to check the actual query
yeah that also.
this could be the error, i will investigate this
the resource/model has enabled soft deletes, is there a way to handle unique validation for soft deletable records