fix: duplicate email/phone shows notification instead of crashing
Two-layer protection on MasterResource and UserResource: 1. Form validation via ->rules() with Rule::unique() scoped to company_id (prevents DB hit in normal flow, shows inline field error) 2. handleRecordCreation/handleRecordUpdate catches UniqueConstraintViolationException as fallback — shows a Filament danger notification and halts without showing an error page Also adds phone uniqueness validation (scoped per tenant). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -4,11 +4,28 @@ namespace App\Filament\Tenant\Resources\MasterResource\Pages;
|
||||
|
||||
use App\Filament\Tenant\Resources\MasterResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Filament\Support\Exceptions\Halt;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\UniqueConstraintViolationException;
|
||||
|
||||
class CreateMaster extends CreateRecord
|
||||
{
|
||||
protected static string $resource = MasterResource::class;
|
||||
|
||||
|
||||
protected function handleRecordCreation(array $data): Model
|
||||
{
|
||||
try {
|
||||
return parent::handleRecordCreation($data);
|
||||
} catch (UniqueConstraintViolationException $e) {
|
||||
$msg = str_contains($e->getMessage(), 'email')
|
||||
? __('Acest email este deja folosit de un alt utilizator.')
|
||||
: __('Acest număr de telefon este deja folosit de un alt utilizator.');
|
||||
|
||||
Notification::make()->danger()->title($msg)->send();
|
||||
|
||||
throw new Halt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user