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
+12 -7
View File
@@ -13,7 +13,7 @@ use Filament\Schemas;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules\Unique;
use Illuminate\Validation\Rule;
class UserResource extends Resource
{
@@ -73,14 +73,19 @@ class UserResource extends Resource
->email()
->required()
->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\TextInput::make('phone')->tel()->maxLength(40),
Forms\Components\TextInput::make('phone')
->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\Select::make('locale')
->options(['ro' => __('Română'), 'ru' => 'Русский', 'en' => 'English'])
->default('ro'),