From 52de3aa0cc29437cf35bada5a01c14d485aea38f Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sat, 31 Jan 2026 12:36:36 +0530 Subject: [PATCH] Enhance CharacteristicApproverMasterResource form with improved validation and field updates --- .../CharacteristicApproverMasterResource.php | 68 +++++++++++++++---- 1 file changed, 54 insertions(+), 14 deletions(-) diff --git a/app/Filament/Resources/CharacteristicApproverMasterResource.php b/app/Filament/Resources/CharacteristicApproverMasterResource.php index afd4d4b..5f7a4ac 100644 --- a/app/Filament/Resources/CharacteristicApproverMasterResource.php +++ b/app/Filament/Resources/CharacteristicApproverMasterResource.php @@ -65,41 +65,63 @@ class CharacteristicApproverMasterResource extends Resource ->afterStateUpdated(function ($state, callable $set) { $set('updated_by', Filament::auth()->user()?->name); }), - Forms\Components\TextInput::make('characteristic_field') - ->label('Master Characteristic Field') - ->columnSpan(2) - ->required() - ->default('NIL') - ->afterStateUpdated(function ($state, callable $set) { - $set('updated_by', Filament::auth()->user()?->name); - }), Forms\Components\TextInput::make('machine_name') ->label('Machine') ->columnSpan(2) + ->reactive() ->required() + ->minLength(5) ->default(function () { return optional(CharacteristicApproverMaster::latest()->first())->machine_name ?? ''; }) ->afterStateUpdated(function ($state, callable $set) { $set('updated_by', Filament::auth()->user()?->name); }), + Forms\Components\TextInput::make('characteristic_field') + ->label('Master Characteristic Field') + ->columnSpan(2) + ->reactive() + ->required() + ->minLength(1) + ->default('NIL') + ->afterStateUpdated(function ($state, callable $set) { + if (strtolower($state) == 'nil' || $state == '' || $state == null) { + $set('characteristic_field', 'NIL'); + } + $set('updated_by', Filament::auth()->user()?->name); + }), Section::make('Approver - 1') // ->description('Prevent abuse by limiting the number of requests per period') ->columnSpan(['default' => 2, 'sm' => 4]) ->schema([ Forms\Components\TextInput::make('name1') ->label('Name') + ->reactive() + ->required() ->afterStateUpdated(function ($state, callable $set) { $set('updated_by', Filament::auth()->user()?->name); }), Forms\Components\TextInput::make('mail1') - ->label('Mail') + ->label('E-Mail') ->columnSpan(['default' => 1, 'sm' => 2]) + ->reactive() + ->required() + ->email() ->afterStateUpdated(function ($state, callable $set) { $set('updated_by', Filament::auth()->user()?->name); }), Forms\Components\TextInput::make('duration1') ->label('Duration (HH.MM)') + ->reactive() + ->required() + ->minLength(4) + ->maxLength(5) + ->regex('/^([0-9]|0[0-9]|1[0-9]|2[0-3])\.(0[0-9]|[1-5][0-9])$/') + ->validationMessages([ + // 'regex' => 'Duration must be 4 digits in HH.MM format (e.g., 12.30, 23.59). Hours: 00-23, Minutes: 00-59.', + 'regex' => 'Duration must be HH.MM format (example: 00.00 - 23.59)', + // 'length' => 'Duration must be exactly 5 characters', + ]) ->afterStateUpdated(function ($state, callable $set) { $set('updated_by', Filament::auth()->user()?->name); }), @@ -111,17 +133,26 @@ class CharacteristicApproverMasterResource extends Resource ->schema([ Forms\Components\TextInput::make('name2') ->label('Name') + ->reactive() ->afterStateUpdated(function ($state, callable $set) { $set('updated_by', Filament::auth()->user()?->name); }), Forms\Components\TextInput::make('mail2') - ->label('Mail') + ->label('E-Mail') ->columnSpan(['default' => 1, 'sm' => 2]) + ->reactive() + ->email() ->afterStateUpdated(function ($state, callable $set) { $set('updated_by', Filament::auth()->user()?->name); }), Forms\Components\TextInput::make('duration2') ->label('Duration (HH.MM)') + ->reactive() + ->length(4) + ->regex('/^([0-9]|0[0-9]|1[0-9]|2[0-3])\.(0[0-9]|[1-5][0-9])$/') + ->validationMessages([ + 'regex' => 'Duration must be HH.MM format (example: 00.00 - 23.59)', + ]) ->afterStateUpdated(function ($state, callable $set) { $set('updated_by', Filament::auth()->user()?->name); }), @@ -133,17 +164,26 @@ class CharacteristicApproverMasterResource extends Resource ->schema([ Forms\Components\TextInput::make('name3') ->label('Name') + ->reactive() ->afterStateUpdated(function ($state, callable $set) { $set('updated_by', Filament::auth()->user()?->name); }), Forms\Components\TextInput::make('mail3') - ->label('Mail') + ->label('E-Mail') ->columnSpan(['default' => 1, 'sm' => 2]) + ->reactive() + ->email() ->afterStateUpdated(function ($state, callable $set) { $set('updated_by', Filament::auth()->user()?->name); }), Forms\Components\TextInput::make('duration3') ->label('Duration (HH.MM)') + ->reactive() + ->length(4) + ->regex('/^([0-9]|0[0-9]|1[0-9]|2[0-3])\.(0[0-9]|[1-5][0-9])$/') + ->validationMessages([ + 'regex' => 'Duration must be HH.MM format (example: 00.00 - 23.59)', + ]) ->afterStateUpdated(function ($state, callable $set) { $set('updated_by', Filament::auth()->user()?->name); }), @@ -205,7 +245,7 @@ class CharacteristicApproverMasterResource extends Resource ->searchable() ->sortable(), Tables\Columns\TextColumn::make('mail1') - ->label('Mail 1') + ->label('E-Mail 1') ->alignCenter() ->searchable() ->sortable(), @@ -219,7 +259,7 @@ class CharacteristicApproverMasterResource extends Resource ->searchable() ->sortable(), Tables\Columns\TextColumn::make('mail2') - ->label('Mail 2') + ->label('E-Mail 2') ->alignCenter() ->searchable() ->sortable(), @@ -233,7 +273,7 @@ class CharacteristicApproverMasterResource extends Resource ->searchable() ->sortable(), Tables\Columns\TextColumn::make('mail3') - ->label('Mail 3') + ->label('E-Mail 3') ->alignCenter() ->searchable() ->sortable(),