Jul 20, 2024
0
Level 8
How to check selected option in selector of action ?
In laravel 10 / filamentphp 3 app I have an action where in disabled TextInput I show current department and selector with Department selection:
Action::make('Move To Department')
->modalIcon(DepartmentHelper::getIcon())
->modalIconColor('success')
->form([
TextInput::make('FromDepartmentId')
->afterStateHydrated(function (TextInput $component, $state) {
$employeeModel = $component->getModelInstance();
if ( ! empty($employeeModel->department)) {
$component->state($employeeModel->department->name);
}
})->disabled()
->label('From department'),
Select::make('move_to_department_id')->label('Move to department')
->prefixIcon(DepartmentHelper::getIcon())
->default(fn(Employee $employee): int => $employee->department_id)
->preload()
->options(Department::query()->getByBranchId(Filament::getTenant()->getAttribute('id'))->pluck('name',
'id'))
->required(),
TextInput::make('move_to_notes')
->required()
->maxLength(500),
])
In which way can I to check that another department is selected?
I tried to use $data of the action :
->action(function (Employee $employee, array $data, array $arguments): void {
// Checking data before making
if ($employee->department_id === $data['move_to_department_id']) {
Notification::make()
->title('Can not move employee to the same department !')
->warning()
->send();
return;
};
// Destination department is selected
if ( ! empty($data['move_to_department_id'])) {
try {
Employee::whereId($employee->id)->update([
'department_id' => $data['move_to_department_id'],
'notes' => $employee->notes . '<br> >>>>>> ' . $data['move_to_notes'] . ' on ' .
DateConv::getFormattedDateTime(Carbon::now(config('app.timezone')),
DatetimeOutputFormat::AS_TEXT),
'updated_at' => Carbon::now(config('app.timezone'))
]);
Notification::make()
->title("Employee's card !")
->body("Employee was moved to selected department !")
->success()
->send();
\Log::info(varDump($data, ' -1 $data::'));
} catch (Exception $e) {
\Log::info($e->getMessage());
}
}
})
->slideOver()
But in this way in case of error modal dialog is closed at first and next error notification is show. I need to leave modal dialog opened and error message inside of modal dialog form (just like as Required works - standart form error).
Please or to participate in this conversation.