Set default value for Builder component

What I am trying to do: Trying to prefill the Builder component. What I did: Try the ->default on the Builder::make, but it doesn't seem to work. My issue/the error: The Builder is not being prefilled. This code does not work:
Builder::make('form')
->blocks([
Block::make('values')->schema([
TextInput::make('percent')
->numeric()
->required()
->placeholder('20'),
])
])
->default([
'type' => 'values',
'data' => [
'percent' => 1,
],
])
->maxItems(1)
->blockNumbers(false)
];
Builder::make('form')
->blocks([
Block::make('values')->schema([
TextInput::make('percent')
->numeric()
->required()
->placeholder('20'),
])
])
->default([
'type' => 'values',
'data' => [
'percent' => 1,
],
])
->maxItems(1)
->blockNumbers(false)
];
4 Replies
awcodes
awcodes10mo ago
The data is an array of arrays. You’re only passing a single array as the default. Also, know that default only works on create pages.
vblinden
vblindenOP10mo ago
An array of arrays also doesn't seem to work:
Builder::make('form')
->blocks([
Block::make('values')->schema([
TextInput::make('percent')
->numeric()
->required()
->placeholder('20'),
])
])
->default([[
'type' => 'values',
'data' => [
'percent' => 1,
],
]])
->maxItems(1)
->blockNumbers(false)
Builder::make('form')
->blocks([
Block::make('values')->schema([
TextInput::make('percent')
->numeric()
->required()
->placeholder('20'),
])
])
->default([[
'type' => 'values',
'data' => [
'percent' => 1,
],
]])
->maxItems(1)
->blockNumbers(false)
I'm checking on the create page indeed
LeandroFerreira
LeandroFerreira10mo ago
this should work. Are you using panel builder or the form builder?
->default([
[
'type' => 'values',
'data' => [
'percent' => '1',
],
],
])
->default([
[
'type' => 'values',
'data' => [
'percent' => '1',
],
],
])
vblinden
vblindenOP10mo ago
Form builder, but I'm using a
$component
->getContainer()
->getComponent('dynamicForm')
->getChildComponentContainer()
->fill();
$component
->getContainer()
->getComponent('dynamicForm')
->getChildComponentContainer()
->fill();
and
Grid::make('input')
->columns(1)
->schema(function (Get $get) use ($rules): array {
if (!$get('class') || ($get('class') && !$rules->has($get('class')))) {
return [];
}

return $rules[$get('class')]['instance']->form();
})
->key('dynamicForm'),
Grid::make('input')
->columns(1)
->schema(function (Get $get) use ($rules): array {
if (!$get('class') || ($get('class') && !$rules->has($get('class')))) {
return [];
}

return $rules[$get('class')]['instance']->form();
})
->key('dynamicForm'),
So I load the code from a class which has the form definition as per my first comment in this thread
Want results from more Discord servers?
Add your server