Need help with unique rule and ignore

Sorry if I can't easily comprehend the documentation. Here's my code:
TextInput::make('personal_email')
->unique('parents','personal_email', $this->model)
TextInput::make('personal_email')
->unique('parents','personal_email', $this->model)
That $this->model is from here:
protected function getTableActions(): array
{
return [
Action::make('edit')
->action(function(Parents $record){
$this->model = $record;
}),
];
}
protected function getTableActions(): array
{
return [
Action::make('edit')
->action(function(Parents $record){
$this->model = $record;
}),
];
}
After doing that I'm still getting error:
The Email has already been taken
The Email has already been taken
I did not change the email.
5 Replies
vahnmarty
vahnmartyOP2y ago
I also want the error message to say "This email is already associated with the other parent. Please use a different email."
Patrick Boivin
Not sure, maybe this?
->unique('parents', 'personal_email', fn () => $this->model)
->unique('parents', 'personal_email', fn () => $this->model)
Also I'm not sure about the value of $this in various places. I think it could be safer to inject the $livewire instance in the closures:
->unique('parents', 'personal_email', fn ($livewire) => $livewire->model)
->unique('parents', 'personal_email', fn ($livewire) => $livewire->model)
->action(function($record, $livewire){
$livewire->model = $record;
}),
->action(function($record, $livewire){
$livewire->model = $record;
}),
etc.
vahnmarty
vahnmartyOP2y ago
That worked! I didn't know I could do fn() => in the parameters. How about the fail message? This email is already associated with the other parent. Please use a different email. Nvm, I saw it .
protected $messages = [
'data.personal_email.unique' => 'This email is already associated with the other parent. Please use a different email.'
];
protected $messages = [
'data.personal_email.unique' => 'This email is already associated with the other parent. Please use a different email.'
];
Patrick Boivin
Nice
awcodes
awcodes2y ago
I think you can just do this too. might be cleaner
->unique('parents', 'personal_email', ignorable: true)
->unique('parents', 'personal_email', ignorable: true)
Want results from more Discord servers?
Add your server