fix: show friendly validation error for duplicate email on user/master create
Added ->unique(ignoreRecord: true) scoped to company_id on the email field in both MasterResource and UserResource. Previously a DB-level UniqueConstraintViolationException crashed the page; now Filament shows "Acest email este deja folosit de un alt utilizator." inline on the field. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -13,6 +13,7 @@ use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\Rules\Unique;
|
||||
|
||||
/**
|
||||
* Tehnicieni — vedere filtrată peste users (role=mechanic).
|
||||
@@ -62,7 +63,17 @@ class MasterResource extends Resource
|
||||
->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('email')->label(__('Email'))->email()->maxLength(120),
|
||||
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),
|
||||
)
|
||||
->validationMessages(['unique' => __('Acest email este deja folosit de un alt utilizator.')]),
|
||||
Forms\Components\Select::make('status')
|
||||
->options(['active' => __('Activ'), 'inactive' => __('Inactiv'), 'blocked' => __('Blocat')])
|
||||
->default('active')
|
||||
|
||||
@@ -13,6 +13,7 @@ use Filament\Schemas;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\Rules\Unique;
|
||||
|
||||
class UserResource extends Resource
|
||||
{
|
||||
@@ -68,7 +69,17 @@ class UserResource extends Resource
|
||||
->columns(2)
|
||||
->schema([
|
||||
Forms\Components\TextInput::make('name')->label(__('Nume'))->required()->maxLength(120),
|
||||
Forms\Components\TextInput::make('email')->email()->required()->maxLength(120),
|
||||
Forms\Components\TextInput::make('email')
|
||||
->email()
|
||||
->required()
|
||||
->maxLength(120)
|
||||
->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),
|
||||
Forms\Components\Select::make('locale')
|
||||
->options(['ro' => __('Română'), 'ru' => 'Русский', 'en' => 'English'])
|
||||
|
||||
Reference in New Issue
Block a user