Added id column to load default plant and line with its validation functionality
This commit is contained in:
@@ -5,8 +5,10 @@ namespace App\Filament\Resources;
|
|||||||
use App\Filament\Resources\ConfigurationResource\Pages;
|
use App\Filament\Resources\ConfigurationResource\Pages;
|
||||||
use App\Filament\Resources\ConfigurationResource\RelationManagers;
|
use App\Filament\Resources\ConfigurationResource\RelationManagers;
|
||||||
use App\Models\Configuration;
|
use App\Models\Configuration;
|
||||||
|
use App\Models\Line;
|
||||||
use Filament\Forms;
|
use Filament\Forms;
|
||||||
use Filament\Forms\Form;
|
use Filament\Forms\Form;
|
||||||
|
use Filament\Forms\Get;
|
||||||
use Filament\Resources\Resource;
|
use Filament\Resources\Resource;
|
||||||
use Filament\Tables;
|
use Filament\Tables;
|
||||||
use Filament\Tables\Table;
|
use Filament\Tables\Table;
|
||||||
@@ -26,20 +28,64 @@ class ConfigurationResource extends Resource
|
|||||||
return $form
|
return $form
|
||||||
->schema([
|
->schema([
|
||||||
Forms\Components\Select::make('plant_id')
|
Forms\Components\Select::make('plant_id')
|
||||||
|
->label('Plant')
|
||||||
->relationship('plant', 'name')
|
->relationship('plant', 'name')
|
||||||
->required()
|
->required()
|
||||||
->reactive(),
|
->reactive()
|
||||||
Forms\Components\Select::make('line_id')
|
->default(function () {
|
||||||
->label('Line')
|
return optional(Configuration::latest()->first())->plant_id;
|
||||||
->required()
|
})
|
||||||
->options(function (callable $get) {
|
->disabled(fn (Get $get) => !empty($get('id')))
|
||||||
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
||||||
$plantId = $get('plant_id');
|
$plantId = $get('plant_id');
|
||||||
if (!$plantId) {
|
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 [];
|
||||||
}
|
}
|
||||||
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')
|
Forms\Components\TextInput::make('c_type')
|
||||||
->label('Type')
|
->label('Type')
|
||||||
->required(),
|
->required(),
|
||||||
@@ -52,6 +98,9 @@ class ConfigurationResource extends Resource
|
|||||||
Forms\Components\TextInput::make('c_value')
|
Forms\Components\TextInput::make('c_value')
|
||||||
->label('Value')
|
->label('Value')
|
||||||
->required(),
|
->required(),
|
||||||
|
Forms\Components\TextInput::make('id')
|
||||||
|
->hidden()
|
||||||
|
->readOnly(),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,9 +4,11 @@ namespace App\Filament\Resources;
|
|||||||
|
|
||||||
use App\Filament\Resources\MachineResource\Pages;
|
use App\Filament\Resources\MachineResource\Pages;
|
||||||
use App\Filament\Resources\MachineResource\RelationManagers;
|
use App\Filament\Resources\MachineResource\RelationManagers;
|
||||||
|
use App\Models\Line;
|
||||||
use App\Models\Machine;
|
use App\Models\Machine;
|
||||||
use Filament\Forms;
|
use Filament\Forms;
|
||||||
use Filament\Forms\Form;
|
use Filament\Forms\Form;
|
||||||
|
use Filament\Forms\Get;
|
||||||
use Filament\Resources\Resource;
|
use Filament\Resources\Resource;
|
||||||
use Filament\Tables;
|
use Filament\Tables;
|
||||||
use Filament\Tables\Table;
|
use Filament\Tables\Table;
|
||||||
@@ -31,21 +33,67 @@ class MachineResource extends Resource
|
|||||||
->label('Plant')
|
->label('Plant')
|
||||||
->relationship('plant', 'name')
|
->relationship('plant', 'name')
|
||||||
->required()
|
->required()
|
||||||
->reactive(),
|
->reactive()
|
||||||
Forms\Components\Select::make('line_id')
|
->default(function () {
|
||||||
->label('Line')
|
return optional(Machine::latest()->first())->plant_id;
|
||||||
->required()
|
})
|
||||||
->options(function (callable $get) {
|
->disabled(fn (Get $get) => !empty($get('id')))
|
||||||
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
||||||
$plantId = $get('plant_id');
|
$plantId = $get('plant_id');
|
||||||
if (!$plantId) {
|
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 [];
|
||||||
}
|
}
|
||||||
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')
|
Forms\Components\TextInput::make('name')
|
||||||
->label('Name')
|
->label('Name')
|
||||||
->required(),
|
->required(),
|
||||||
|
Forms\Components\TextInput::make('id')
|
||||||
|
->hidden()
|
||||||
|
->readOnly(),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user