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:
2026-09-26 20:16:13 +00:00
parent fcbb807462
commit f1afcafa5d
6 changed files with 96 additions and 15 deletions
@@ -13,7 +13,7 @@ use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules\Unique;
use Illuminate\Validation\Rule;
/**
* Tehnicieni — vedere filtrată peste users (role=mechanic).
@@ -62,16 +62,22 @@ class MasterResource extends Resource
->columns(2)
->schema([
Forms\Components\TextInput::make('name')->label(__('Nume'))->required()->maxLength(120),
Forms\Components\TextInput::make('phone')->label(__('Telefon'))->tel()->maxLength(40),
Forms\Components\TextInput::make('phone')
->label(__('Telefon'))
->tel()
->maxLength(40)
->rules(fn ($record) => $record
? [Rule::unique('users', 'phone')->where('company_id', auth()->user()?->company_id)->ignore($record->id)]
: [Rule::unique('users', 'phone')->where('company_id', auth()->user()?->company_id)]
)
->validationMessages(['unique' => __('Acest număr de telefon este deja folosit de un alt utilizator.')]),
Forms\Components\TextInput::make('email')
->label(__('Email'))
->email()
->maxLength(120)
->unique(
table: 'users',
column: 'email',
ignoreRecord: true,
modifyRuleUsing: fn (Unique $rule) => $rule->where('company_id', auth()->user()?->company_id),
->rules(fn ($record) => $record
? [Rule::unique('users', 'email')->where('company_id', auth()->user()?->company_id)->ignore($record->id)]
: [Rule::unique('users', 'email')->where('company_id', auth()->user()?->company_id)]
)
->validationMessages(['unique' => __('Acest email este deja folosit de un alt utilizator.')]),
Forms\Components\Select::make('status')