pre-populating complex fields with set_min()
By setting ->set_min() I thought a complex field would be pre-populated with declared minimum amount of field rows. But even with ->set_required(true) the user must add a row himself. If this is the intended behavior I found it counterintuitive or at least it lacks this little step of comfort. If it isn’t, It’s perhaps a bug.
On the other hand I’m perhaps missing cases where it is better not to setup the field-rows for the user. In this case could you point me to a solution for pre-populting complex fields with a certain amount of rows (empty of course ;)? Thanks!
On the PHP side I tried set_default_value() but I see now the Complex_Field class has no such method. I looked also if there is any hook for pre-populating a complex field but I’ve found none (feature?). The data for complex fields is taken directly and only from the db by load_values_from_db().
On the JS side I’ve found the 'field:rendered' event and that the groupsCollection is listening to the 'add' event. So I see a possibility to call an 'add' event after the fields have been rendered. Could you give me a hint to build upon this?
Hi @boquiabierto,
I think that there is no need to involve any PHP to achieve this. You can just modify the Complex Field JS view and add a group X number of times in the groupsCollection if its empty, where X is the set_min value. Now sure if this is the only thing that needs to be done, because the Complex field is... complex :)
We might add an additional parameter to the set_min method that will enable the groups pre-population, but its not a high priority feature. Meanwhile keep us posted if you found a solution!
2.0.0 has been released which allows you to use ->set_default_value() for complex fields thus achieving prefilled groups.
An example is available here: https://carbonfields.net/docs/guides-predefined-complex-groups/?crb_version=2-0-0