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
In the documentation it says this:
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!
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!
message.txt5.27KB
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
So, your afterStateUpdated callback is going to need $get, $set and $state.
So setting the repeater would just be something like