Form issues dynamically loadin static functions from a class that return an array of form components
I'm dynamically loading form compnents into schema(). The fields are displayed properly but are not operational (validation, default, saving - all not working).
When I load the static function from the class manually in code, everything is working as expected.
Forms\Components\Fieldset::make('input_settings')
->label(__('Settings'))
->statePath('settings')
->visible(function (Get $get, Component $component) {
if ($get('type')) {
return true;
} else {
return false;
}
})
->schema(function (Get $get, Component $component) {
// this works fine
return \App\Services\Workflows\InputTypes\FilesUpload::settingsForm() ?? [];
// this only display the form, but doesn't save the data (even default values don't work)
if ($type = $get('type')) {
$types = InputTypes::list();
$type_class = $types[$type];
//dd($type_class); // returns "App\Services\Workflows\InputTypes\FilesUpload"
$form = $type_class::settingsForm();
} else {
$form = [];
}
return $form;
})
Forms\Components\Fieldset::make('input_settings')
->label(__('Settings'))
->statePath('settings')
->visible(function (Get $get, Component $component) {
if ($get('type')) {
return true;
} else {
return false;
}
})
->schema(function (Get $get, Component $component) {
// this works fine
return \App\Services\Workflows\InputTypes\FilesUpload::settingsForm() ?? [];
// this only display the form, but doesn't save the data (even default values don't work)
if ($type = $get('type')) {
$types = InputTypes::list();
$type_class = $types[$type];
//dd($type_class); // returns "App\Services\Workflows\InputTypes\FilesUpload"
$form = $type_class::settingsForm();
} else {
$form = [];
}
return $form;
})
1 Reply
So apparently this was never meant to work 🙂
My solution:
Forms\Components\Tabs\Tab::make('Input')
->statePath('input')
->schema([
Forms\Components\Select::make('type')
->options(InputTypes::listOptions())
->required()
->live()
->afterStateUpdated(function ($state) {
if ($state) {
$types = InputTypes::list();
$type_class = $types[$state];
self::$input_type_class = $type_class;
}
}),
... self::getInputSettingsFieldsets(), // instead of the code above
]),
public static function getInputSettingsFieldsets(): array
{
$forms = [];
$input_types = InputTypes::list();
foreach ($input_types as $id => $input_type_class) {
$forms[] = Forms\Components\Fieldset::make('input_settings')
->label(__('Settings'))
->statePath('settings')
->visible(function (Get $get) use ($id) {
if ($get('type') == $id) {
return true;
} else {
return false;
}
})
->schema(function () use ($input_type_class) {
return $input_type_class::settingsForm() ?? [];
});
}
return $forms;
}
Forms\Components\Tabs\Tab::make('Input')
->statePath('input')
->schema([
Forms\Components\Select::make('type')
->options(InputTypes::listOptions())
->required()
->live()
->afterStateUpdated(function ($state) {
if ($state) {
$types = InputTypes::list();
$type_class = $types[$state];
self::$input_type_class = $type_class;
}
}),
... self::getInputSettingsFieldsets(), // instead of the code above
]),
public static function getInputSettingsFieldsets(): array
{
$forms = [];
$input_types = InputTypes::list();
foreach ($input_types as $id => $input_type_class) {
$forms[] = Forms\Components\Fieldset::make('input_settings')
->label(__('Settings'))
->statePath('settings')
->visible(function (Get $get) use ($id) {
if ($get('type') == $id) {
return true;
} else {
return false;
}
})
->schema(function () use ($input_type_class) {
return $input_type_class::settingsForm() ?? [];
});
}
return $forms;
}