Setting the default value of a TextInput within a Repeater

Hello, I'm trying to set the default value of TextInput within a Repeater using another field outside this repeater. This is a simplified example of what I'm doing, unfortunately I'm not getting the value of cap field. Any suggestions? public static function form(Form $form): Form { return $form ->schema([ HiddenInput::make('cap'), Repeater::make('numbers') ->schema([ TextInput::make('prefix')->default(fn (Get $get) => $get('../../cap')), // I will add a query that will use the value to retrieve data from DB ]) ]); }
Solution:
Do you want to do this? ```php Forms\Components\Select::make('select') ->options([...
Jump to solution
14 Replies
spacedev
spacedevOP6mo ago
I can do that, but what about the TextInput in the Repeater? What can I do to set is value?
LeandroFerreira
LeandroFerreira6mo ago
using default, this is correct. Default only works in the create page
Hidden::make('cap')
->default('something'),
Repeater::make('numbers')
->schema([
TextInput::make('prefix')->default(fn (Get $get) => $get('../../cap'))
])
Hidden::make('cap')
->default('something'),
Repeater::make('numbers')
->schema([
TextInput::make('prefix')->default(fn (Get $get) => $get('../../cap'))
])
spacedev
spacedevOP6mo ago
Yes, I'm using it in the create page. If I set a default value to the cap field I'm able to get it from prefix field. But what if cap field is set dynamically? Is there a way to set a repeater field value from outside the repeater?
LeandroFerreira
LeandroFerreira6mo ago
But what if cap field is set dynamically? How are you doing this?
spacedev
spacedevOP6mo ago
I have a Select field, and in the "afterStateUpdated" I set the "cap" field depending on which option the user has selected in the Select field
LeandroFerreira
LeandroFerreira6mo ago
afterStateUpdated you want to set all fields in the repeater with this value, right?
spacedev
spacedevOP6mo ago
I was thinking of setting the repeater field based on the cap field, I mean within the repeater, using "Get". Anyway, setting the repeater field from the afterStateUpdated of the first Select field or the CAP TexInput would be ok.
oks_voice123
oks_voice1236mo ago
we have more or less the same request , except that for me it is in a table and that I want to copy the value of a specific ( let's say Column B) into column A if A is empty so put the value of B as placeholder for example
spacedev
spacedevOP6mo ago
I think the problem is related to ->default() method This one does not work TextInput::make('prefix')->default(fn (Get $get) => $get('../../cap')) This one does work, I'm able to get cap from outside the repeater Select::make('prefix')->options(function(Get $get){return ['cap' => $get('../../cap')];})
Solution
LeandroFerreira
LeandroFerreira6mo ago
Do you want to do this?
Forms\Components\Select::make('select')
->options([
'option1' => 'Option 1',
'option2' => 'Option 2',
'option3' => 'Option 3',
])
->live()
->afterStateUpdated(function (Forms\Components\Select $component, ?string $state) {
$repeater = $component->getContainer()->getComponent('data.repeater');

$repeater->state(Arr::map($repeater->getState(), function (array $item) use ($state) {
$item['prefix'] = $state;

return $item;
}));
}),

Forms\Components\Repeater::make('repeater')
->schema([
Forms\Components\TextInput::make('prefix')
->default(fn(Forms\Get $get): ?string => $get('../../select'))
]),
Forms\Components\Select::make('select')
->options([
'option1' => 'Option 1',
'option2' => 'Option 2',
'option3' => 'Option 3',
])
->live()
->afterStateUpdated(function (Forms\Components\Select $component, ?string $state) {
$repeater = $component->getContainer()->getComponent('data.repeater');

$repeater->state(Arr::map($repeater->getState(), function (array $item) use ($state) {
$item['prefix'] = $state;

return $item;
}));
}),

Forms\Components\Repeater::make('repeater')
->schema([
Forms\Components\TextInput::make('prefix')
->default(fn(Forms\Get $get): ?string => $get('../../select'))
]),
spacedev
spacedevOP6mo ago
I will try this way thank you
LeandroFerreira
LeandroFerreira6mo ago
spacedev
spacedevOP6mo ago
@Leandro Ferreira Thank you very much for your help! It works great using your code
Want results from more Discord servers?
Add your server