F
Filamentβ€’2mo ago
Maxime

Programmatically fill repeaters in EditRecord page

Is there a way to fill repeaters in an EditRecord page with some values ? Using ->default() is only possible on a CreateRecord page IIRC. I've tried directly using
$form->fill([
'repeated_field' => [
['key' => 'value'],
['key' => 'value'],
...
]
]);
$form->fill([
'repeated_field' => [
['key' => 'value'],
['key' => 'value'],
...
]
]);
but nothing gets added to the repeater, and if i dd() the form raw state right after, this is what i get :
array:1 [
"repeated_field" => []
]
array:1 [
"repeated_field" => []
]
11 Replies
krekas
krekasβ€’2mo ago
form gets filled by filament. I believe you should use mutateFormDataBeforeFill() or hooks check the source code for the edit page how it is filled https://github.com/filamentphp/filament/blob/3.x/packages/panels/src/Resources/Pages/EditRecord.php#L104
Maxime
MaximeOPβ€’2mo ago
I've tried the $form->fill() and the mutateFormDataBeforeFill(), but my repeated field array just get emptied Update : i've realised that this is working if i don't use ->relationship() on my repeater. Any idea on why the relationship would break that ?
krekas
krekasβ€’2mo ago
Filament tries to get data from a relationship I guess
Maxime
MaximeOPβ€’2mo ago
Yep, it looks like it. Maybe i'm not using the right pattern to begin with ? I want, from an already existing filament resource, to be able to create items through a HasMany relationship, with a pre-defined repeater of 3 items and some pre filled attributes. Do you think extending EditRecord suits this ? (I'm doing this right now)
krekas
krekasβ€’2mo ago
For me it's weird that you are doing this on the edit page
CodeWithDennis
CodeWithDennisβ€’2mo ago
Not sure if this helps, but i have a custom action with a repeater that gets filled this way.
->fillForm(fn (MyModel $record) => [
'attributes' => $record->attributes->map(fn ($attribute) => [
'key' => $attribute->key,
'value' => $attribute->value,
])->toArray(),
])
->fillForm(fn (MyModel $record) => [
'attributes' => $record->attributes->map(fn ($attribute) => [
'key' => $attribute->key,
'value' => $attribute->value,
])->toArray(),
])
Maxime
MaximeOPβ€’2mo ago
Thank you for your snippet, i think this would work great if i need to edit them, unfortunately i'm not sure how to use it to 'create' new repeater items
CodeWithDennis
CodeWithDennisβ€’2mo ago
Wouldn't that just be?
->default([
['key' => 'test', 'value' => 'test'], // Change with your own fields and values.
])
->default([
['key' => 'test', 'value' => 'test'], // Change with your own fields and values.
])
Edit: Never mind this is on the create πŸ˜…
krekas
krekasβ€’2mo ago
It's weird how you want to use it. Maybe give more context and code
Maxime
MaximeOPβ€’2mo ago
Thank you all for your help, I ended up removing the relationship and using handleRecordUpdate to manage my update
Want results from more Discord servers?
Add your server