F
Filament3mo ago
wazkaz

Unique validation on edit throws error

I have a form field which have unique validation. When I am trying to update the record it throws the unique validation error. The documentation mentioned to add the ->unique(ignoreRecord: true) but didn't helped. Anyone face this issue before? I also tried this ->unique(ignorable: fn ($record) => $record) but same result.
Solution:
I found what was the problem. So the $record somehow was null and that's why validated the unique every time. The workaround for this I passed the record from the Livewire component:
use Livewire\Component as Livewire;
->unique(ignorable: fn (Livewire $livewire) => $livewire->record)
use Livewire\Component as Livewire;
->unique(ignorable: fn (Livewire $livewire) => $livewire->record)
...
Jump to solution
11 Replies
toeknee
toeknee3mo ago
What's the error? What's the whole field code?
wazkaz
wazkazOP3mo ago
No description
No description
toeknee
toeknee3mo ago
->unique(ignoreRecord: true)
->unique(ignoreRecord: true)
should suffice since it checks this record only. Are you sure you don't have any other records with that value? What if you try without spaces?
wazkaz
wazkazOP3mo ago
I already tried this: ->unique(ignoreRecord: true) but still throws the error. The other records have different values. I tried without space, it saved. Then I tried to update the record again and still throws the error.
toeknee
toeknee3mo ago
So the error is down to spaces
wazkaz
wazkazOP3mo ago
But the value without spaces still throws error or what do you mean?
toeknee
toeknee3mo ago
Ok check the queries as that deosn't make sense.
wazkaz
wazkazOP3mo ago
Btw, I am using this ->unique(ignorable: fn ($record) => $record) on other places and it is working there with and without spaces
toeknee
toeknee3mo ago
So it works and something isn't working with your setup. Use debugbar/Telescope and view the validation request to see the query run as to what's happening
wazkaz
wazkazOP3mo ago
okay i will check
Solution
wazkaz
wazkaz3mo ago
I found what was the problem. So the $record somehow was null and that's why validated the unique every time. The workaround for this I passed the record from the Livewire component:
use Livewire\Component as Livewire;
->unique(ignorable: fn (Livewire $livewire) => $livewire->record)
use Livewire\Component as Livewire;
->unique(ignorable: fn (Livewire $livewire) => $livewire->record)
Want results from more Discord servers?
Add your server