FilamentF
Filament3y ago
AxlF

Add Items to Repeater with a Modal

Hey,

I'm pretty sure it could work, but I don't get how. Basically I would like to add Items to a Repeater with a form in a Modal instead of an inline form.

The modal should ask for postcode and city. The Repeater should show a compound string ("$postcode $city").

I get a modal as input form, but the values are not shown in the repeater

Repeater::make('location')
 ->schema([
    TextInput::make('location'),
  ])
  ->addAction(function (Action $action) {
      return $action->form([
          TextInput::make('postcode')
            ->required(),
          TextInput::make('city')
            ->required(),
      ]);
  }),
`
Solution
Repeater::make('locations')
->schema([
    Placeholder::make('location')
        ->label('')
        ->content(fn (Get $get) => $get('postcode').' '.$get('city')),
])
->addAction(function ($action) {
    return $action->form([
            TextInput::make('postcode'),
            TextInput::make('city'),
        ])
        ->action(function ($data, Set $set, Get $get) {
            $currentState = $get('locations') ?? [];
            $result = array_merge($currentState, [$data]);
            $set('locations', $result);
        });
})
Was this page helpful?