F
Filament3mo ago
nowak

Setting defaulItems count dynamically in repeater field

I have a repeater field for product SKU's, that I want to predefine the items for depending on the product type and attribute. I have attached my ProductResource form that clarifies what I am trying to achieve. So in the defaultItems() method I am attempting to set the number based on the input of the product_type_id field and the attribute_id field. But it seems like it only returns the output of the initial function run, and not upon later changes to the input fields. In the documentation it says this:
Note that these default items are only created when the form is loaded without existing data. Inside panel resources this only works on Create Pages, as Edit Pages will always fill the data from the model.
https://filamentphp.com/docs/3.x/forms/fields/repeater#setting-empty-default-items Which makes me think that live setting of defaultItems() is not possible, since it might only be triggered when the form is loaded? I might be mistaken, but this is the only explanation I have for the behaviour I see. Is there another way to set the item count of a repeater field dynamically, like I set addable() and deletable()? Or if using another approach than repeater fields would be more appropriate for my use case, then please let me know! Thank you!
Solution:
On both of the live() fields you call afterStateUpdated() and in the callback you $get the value of the other field, do your check to make sure they both have values then $set() the repeater field. So, your afterStateUpdated callback is going to need $get, $set and $state. So setting the repeater would just be something like ...
Jump to solution
5 Replies
awcodes
awcodes3mo ago
Yea. I don’t think you’re going to be able to do this with default(). Only thing that comes to mind initially is setting the repeater’s state to an array of empty result arrays. Not 100% sure how that will play out though since it’s set up as a relationship.
nowak
nowak3mo ago
I just found this comment from Dan on a Github issue which is basically the same problem as I have: https://github.com/filamentphp/filament/issues/5771#issuecomment-1442475275
The default items get loaded when the form is first loaded. You need to use afterStateUpdated() to $set the repeater with the correct number of items.
But how would I use afterStateUpdated() to $set the repeater with the correct number of items? I know how to use afterStateUpdated() and $set, but what variable or field should I set to define the repeater items?
GitHub
Make default items dynamically change based on reactive input numbe...
Package filament/filament Package Version v2.0.0 Laravel Version v9.19.0 Livewire Version v2.11 PHP Version PHP 8.0 Problem description When changing the value of a reactive field, the default numb...
Solution
awcodes
awcodes3mo ago
On both of the live() fields you call afterStateUpdated() and in the callback you $get the value of the other field, do your check to make sure they both have values then $set() the repeater field. So, your afterStateUpdated callback is going to need $get, $set and $state. So setting the repeater would just be something like
$set(‘repeatername’, [
[‘name’ => null],
[‘name’ => null],
…repeat for the number of items you need
])
$set(‘repeatername’, [
[‘name’ => null],
[‘name’ => null],
…repeat for the number of items you need
])
nowak
nowak3mo ago
Thanks, I will give it a try! It works! Thank you again 🙂
awcodes
awcodes3mo ago
Awesome, glad you got it working. Please mark it as the answer so others can find the solution.