F
Filamentβ€’2mo ago
SOIX

default value on dynamically added form fields

Forms\Components\Group::make(
function (Get $get) {
$collection = $get('component.collection') ?? [];

return collect($collection)
->filter(function ($componentClass) {
// Ensure the component class is valid
return class_exists($componentClass);
})
->map(function ($componentClass, $key) {
// Dynamically resolve the component class and return its schema
return Forms\Components\Section::make($componentClass::$title)
->collapsible()
->schema($componentClass::getSchema($key)); // Show only if it's the current component
})
->toArray(); // Ensure it's an array of components
}
)->live()
Forms\Components\Group::make(
function (Get $get) {
$collection = $get('component.collection') ?? [];

return collect($collection)
->filter(function ($componentClass) {
// Ensure the component class is valid
return class_exists($componentClass);
})
->map(function ($componentClass, $key) {
// Dynamically resolve the component class and return its schema
return Forms\Components\Section::make($componentClass::$title)
->collapsible()
->schema($componentClass::getSchema($key)); // Show only if it's the current component
})
->toArray(); // Ensure it's an array of components
}
)->live()
And one of component classes have:
public static function getSchema (string $key): array
{
return [
Forms\Components\TextInput::make($key . '.title')
->default('Some default text')
->required()
];
}
public static function getSchema (string $key): array
{
return [
Forms\Components\TextInput::make($key . '.title')
->default('Some default text')
->required()
];
}
Some default text is not appearing.. Anyone know why? Thanks
2 Replies
toeknee
toekneeβ€’2mo ago
You can't set a default field on dynamics since defaults are populated once the dom has loaded and not again. You would need to use $set() to set defaults post load. maybe with a $get to check it is empty.
SOIX
SOIXOPβ€’2mo ago
i've found an ugly workaround, but thanks πŸ˜„
Want results from more Discord servers?
Add your server