F
Filament5mo 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
4 Replies
toeknee
toeknee5mo 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
SOIXOP5mo ago
i've found an ugly workaround, but thanks 😄
Arthur Monney
Arthur Monney3mo ago
@SOIX can you share you solution please? A face the same issue
SOIX
SOIXOP3mo ago
Well I predefined hidden fields for default values and then populated dynamic element like this:
$this->form($formFields)
->icon('heroicon-s-paint-brush')
->action(function (Set $set, $data) use ($prefix, $formFields) {
foreach ($formFields as $field => $value) {
$set($prefix . '.animation', $data[$field]);
}
})->mountUsing(function (Form $form, Forms\Get $get) use ($prefix, $formFields) {
$existingData = [];

foreach ($formFields as $field => $value) {
$existingData[$field] = $get($prefix . '.' . $field) ?? '';
}

$form->fill($existingData);
});
$this->form($formFields)
->icon('heroicon-s-paint-brush')
->action(function (Set $set, $data) use ($prefix, $formFields) {
foreach ($formFields as $field => $value) {
$set($prefix . '.animation', $data[$field]);
}
})->mountUsing(function (Form $form, Forms\Get $get) use ($prefix, $formFields) {
$existingData = [];

foreach ($formFields as $field => $value) {
$existingData[$field] = $get($prefix . '.' . $field) ?? '';
}

$form->fill($existingData);
});
So basically populate dynamic form with mountUsing. Now I guess i needed to hnave predefined keys in form, so thats why i haave hidden fields, but if u can find other way you can remove it.

Did you find this page helpful?