Added id column to load default plant and line with its validation functionality

This commit is contained in:
dhanabalan
2025-05-31 16:02:08 +05:30
parent e8bac66e51
commit 376d0800dd
2 changed files with 115 additions and 18 deletions

View File

@@ -5,8 +5,10 @@ namespace App\Filament\Resources;
use App\Filament\Resources\ConfigurationResource\Pages;
use App\Filament\Resources\ConfigurationResource\RelationManagers;
use App\Models\Configuration;
use App\Models\Line;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
@@ -26,20 +28,64 @@ class ConfigurationResource extends Resource
return $form
->schema([
Forms\Components\Select::make('plant_id')
->label('Plant')
->relationship('plant', 'name')
->required()
->reactive(),
Forms\Components\Select::make('line_id')
->label('Line')
->required()
->options(function (callable $get) {
->reactive()
->default(function () {
return optional(Configuration::latest()->first())->plant_id;
})
->disabled(fn (Get $get) => !empty($get('id')))
->afterStateUpdated(function ($state, callable $set, callable $get) {
$plantId = $get('plant_id');
if (!$plantId) {
$set('cPlantError', 'Please select a plant first.');
return;
}
else
{
$set('cPlantError', null);
}
})
->extraAttributes(fn ($get) => [
'class' => $get('cPlantError') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('cPlantError') ? $get('cPlantError') : null)
->hintColor('danger'),
Forms\Components\Select::make('line_id')
->label('Line')
->relationship('line', 'name')
->required()
->reactive()
->options(function (callable $get) {
if (!$get('plant_id')) {
return [];
}
return \App\Models\Line::where('plant_id', $plantId)->pluck('name', 'id')->toArray();
return Line::where('plant_id', $get('plant_id'))
->pluck('name', 'id')
->toArray();
})
->reactive(),
->default(function () {
return optional(Configuration::latest()->first())->line_id;
})
->disabled(fn (Get $get) => !empty($get('id')))
->afterStateUpdated(function ($state, callable $set, callable $get) {
$lineId = $get('line_id');
if (!$lineId) {
$set('cLineError', 'Please select a line first.');
return;
}
else
{
$set('cLineError', null);
}
})
->extraAttributes(fn ($get) => [
'class' => $get('cLineError') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('cLineError') ? $get('cLineError') : null)
->hintColor('danger'),
Forms\Components\TextInput::make('c_type')
->label('Type')
->required(),
@@ -52,6 +98,9 @@ class ConfigurationResource extends Resource
Forms\Components\TextInput::make('c_value')
->label('Value')
->required(),
Forms\Components\TextInput::make('id')
->hidden()
->readOnly(),
]);
}

View File

@@ -4,9 +4,11 @@ namespace App\Filament\Resources;
use App\Filament\Resources\MachineResource\Pages;
use App\Filament\Resources\MachineResource\RelationManagers;
use App\Models\Line;
use App\Models\Machine;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
@@ -31,21 +33,67 @@ class MachineResource extends Resource
->label('Plant')
->relationship('plant', 'name')
->required()
->reactive(),
Forms\Components\Select::make('line_id')
->label('Line')
->required()
->options(function (callable $get) {
->reactive()
->default(function () {
return optional(Machine::latest()->first())->plant_id;
})
->disabled(fn (Get $get) => !empty($get('id')))
->afterStateUpdated(function ($state, callable $set, callable $get) {
$plantId = $get('plant_id');
if (!$plantId) {
$set('mPlantError', 'Please select a plant first.');
return;
}
else
{
$set('mPlantError', null);
}
})
->extraAttributes(fn ($get) => [
'class' => $get('mPlantError') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('mPlantError') ? $get('mPlantError') : null)
->hintColor('danger'),
Forms\Components\Select::make('line_id')
->label('Line')
->relationship('line', 'name')
->required()
->reactive()
->options(function (callable $get) {
if (!$get('plant_id')) {
return [];
}
return \App\Models\Line::where('plant_id', $plantId)->pluck('name', 'id')->toArray();
return Line::where('plant_id', $get('plant_id'))
->pluck('name', 'id')
->toArray();
})
->reactive(),
->default(function () {
return optional(Machine::latest()->first())->line_id;
})
->disabled(fn (Get $get) => !empty($get('id')))
->afterStateUpdated(function ($state, callable $set, callable $get) {
$lineId = $get('line_id');
if (!$lineId) {
$set('mLineError', 'Please select a line first.');
return;
}
else
{
$set('mLineError', null);
}
})
->extraAttributes(fn ($get) => [
'class' => $get('mLineError') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('mLineError') ? $get('mLineError') : null)
->hintColor('danger'),
Forms\Components\TextInput::make('name')
->label('Name')
->required(),
Forms\Components\TextInput::make('id')
->hidden()
->readOnly(),
]);
}