fix: phone uniqueness validation and DB constraint for users
- Replaced broken ->rules(fn (\$record)) with proper Filament ->unique() using ignoreRecord:true + modifyRuleUsing scoped to company_id, applied to both phone and email in MasterResource and UserResource - Added migration 2026_09_26_000001 to add unique(company_id, phone) constraint on users table (nulls out duplicate phones before adding, MySQL allows multiple NULLs in unique index so blank phones are fine) - Exception catch in Create/EditMaster and Create/EditUser now also handles UniqueConstraintViolationException on phone Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -14,6 +14,7 @@ use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Validation\Rules\Unique;
|
||||
|
||||
class UserResource extends Resource
|
||||
{
|
||||
@@ -73,17 +74,21 @@ class UserResource extends Resource
|
||||
->email()
|
||||
->required()
|
||||
->maxLength(120)
|
||||
->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)]
|
||||
->unique(
|
||||
table: 'users',
|
||||
column: 'email',
|
||||
ignoreRecord: true,
|
||||
modifyRuleUsing: fn (Unique $rule) => $rule->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)
|
||||
->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)]
|
||||
->unique(
|
||||
table: 'users',
|
||||
column: 'phone',
|
||||
ignoreRecord: true,
|
||||
modifyRuleUsing: fn (Unique $rule) => $rule->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')
|
||||
|
||||
Reference in New Issue
Block a user