Enhance RequestCharacteristicResource form with validation and new fields
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled

This commit is contained in:
dhanabalan
2026-01-31 12:34:44 +05:30
parent dec4900659
commit 02c9059cf3

View File

@@ -8,6 +8,7 @@ use App\Models\CharacteristicApproverMaster;
use App\Models\Item; use App\Models\Item;
use App\Models\Machine; use App\Models\Machine;
use App\Models\RequestCharacteristic; use App\Models\RequestCharacteristic;
use Closure;
use Filament\Facades\Filament; use Filament\Facades\Filament;
use Filament\Forms; use Filament\Forms;
use Filament\Forms\Components\Section; use Filament\Forms\Components\Section;
@@ -21,6 +22,7 @@ use Filament\Tables\Actions\ImportAction;
use Filament\Tables\Table; use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope; use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Facades\Schema;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;
class RequestCharacteristicResource extends Resource class RequestCharacteristicResource extends Resource
@@ -236,7 +238,7 @@ class RequestCharacteristicResource extends Resource
}) })
->required(), ->required(),
// ->disabled(fn ($get) => self::isFieldDisabled($get)) // ->disabled(fn ($get) => self::isFieldDisabled($get))
Section::make('Request Characteristic') Section::make('Request Characteristic Field')
// ->columnSpan(['default' => 2, 'sm' => 4]) // ->columnSpan(['default' => 2, 'sm' => 4])
->reactive() ->reactive()
->schema([ ->schema([
@@ -244,24 +246,82 @@ class RequestCharacteristicResource extends Resource
->label('Characteristic Name') ->label('Characteristic Name')
->reactive() ->reactive()
->afterStateUpdated(function ($state, callable $set, callable $get) { ->afterStateUpdated(function ($state, callable $set, callable $get) {
$set('characteristic_name', strtoupper($state));
$set('updated_by', Filament::auth()->user()?->name); $set('updated_by', Filament::auth()->user()?->name);
}) })
->required() ->required()
->disabled(fn ($get) => self::isFieldDisabled($get)), ->rules([
// function (callable $get) {
// return Rule::unique('request_characteristics', 'characteristic_name')
// ->where('plant_id', $get('plant_id'))
// ->where('machine_id', $get('machine_id'))
// ->where('machine_name', $get('machine_name'))
// ->ignore($get('id'));
// },
function (callable $get): Closure {
return function (string $attribute, $value, Closure $fail) use ($get) {
$charVal = strtolower($value);
$columns = Schema::getColumnListing('class_characteristics');
if (! in_array($charVal, $columns, true)) {
$fail('Unknown characteristic field found.'); // .implode(', ', $columns).
} else {
$plantId = $get('plant_id');
$jobNo = $get('aufnr');
$updId = $get('id');
$pendingExists = RequestCharacteristic::where('plant_id', $plantId)->where('aufnr', $jobNo)->where('characteristic_name', $value)->latest()->first();
if ($pendingExists) {
if ($updId && $pendingExists->id == $updId) {
return;
}
$app1 = $pendingExists->approver_status1 ?? null;
$app2 = $pendingExists->approver_status2 ?? null;
$app3 = $pendingExists->approver_status3 ?? null;
if ($app1 != 'Rejected' && $app2 != 'Rejected' && $app3 != 'Rejected' && $app1 != 'Approved' && $app2 != 'Approved' && $app3 != 'Approved') {
$fail('Approval is already pending.');
}
}
}
};
},
])
->readOnly(fn ($get) => self::isFieldDisabled($get)), // disabled
Forms\Components\TextInput::make('current_value') Forms\Components\TextInput::make('current_value')
->label('Current Value') ->label('Current Value')
->reactive() ->reactive()
->afterStateUpdated(function ($state, callable $set, callable $get) { ->afterStateUpdated(function ($state, callable $set, callable $get) {
$set('updated_by', Filament::auth()->user()?->name); $set('updated_by', Filament::auth()->user()?->name);
}) })
->disabled(fn ($get) => self::isFieldDisabled(get: $get)), ->required(function (callable $get) {
$updateVal = $get('update_value');
if ($updateVal == null || $updateVal == '') {
return true;
}
return false;
})
->readOnly(fn ($get) => self::isFieldDisabled($get)),
Forms\Components\TextInput::make('update_value') Forms\Components\TextInput::make('update_value')
->label('Update Value') ->label('Update Value')
->reactive() ->reactive()
->afterStateUpdated(function ($state, callable $set, callable $get) { ->afterStateUpdated(function ($state, callable $set, callable $get) {
$set('updated_by', Filament::auth()->user()?->name); $set('updated_by', Filament::auth()->user()?->name);
}) })
->disabled(fn ($get) => self::isFieldDisabled($get)), ->rules([
function (callable $get): Closure {
return function (string $attribute, $value, Closure $fail) use ($get) {
$currVal = $get('current_value');
if ($value == $currVal) {
$fail('Update value must be different from current value.');
}
};
},
])
->readOnly(fn ($get) => self::isFieldDisabled($get)),
]) ])
->collapsible() ->collapsible()
->columns(['default' => 1, 'sm' => 3]), ->columns(['default' => 1, 'sm' => 3]),
@@ -798,6 +858,19 @@ class RequestCharacteristicResource extends Resource
->dateTime() ->dateTime()
->searchable() ->searchable()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('mail_status')
->label('Mail Status')
->alignCenter()
->searchable()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('trigger_at')
->label('Trigger At')
->dateTime()
->alignCenter()
->searchable()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('created_at') Tables\Columns\TextColumn::make('created_at')
->label('Created At') ->label('Created At')
->dateTime() ->dateTime()