Note: From Joomla 3.6 Subform form field is introduced and repeatbale form field will be removed from Joomla 4.0. So if you are developing for j3.6 or upper then use subform form field.
Joomla 3.2 added a nice feature introducing repeatable standard form field. Now it’s easy to add repeat fields just defined from the standard xml file of any joomla extensions. For each repeat box you can add multiple fields (joomla standard form fields ), means you can add fields under fields !
Now we will go more technical but will try our best to write it for new joomla developer.
Write a new field in xml like any other standard field. Here the only new thing is field type “Repeatable” and icon
Now add hidden fieldset inside the field and the field set name should be your field name _modal , means here the hidden fieldset name we need to add myrepeatfield_modal and it confirms the modal window (in which you will actually get the repeatable fields interface) will have the html id attribute myrepeatfield_modal_table.
If you are trying to add repeat field in a module then please save the module edit window and see the field you add in step one should have a list icon and click it, it will open a modal and you will see a first blank text field, there will be */- buttons to add more or delete. Try yourself and save, now save the module edit window again, now click the field list icon again to open the popup and see you have those fields !
The way we added a hidden field set and inside it we added a field named customfield1 we can add more standard joomla form fields.
If you finished up to step 3 then I think you already confident to use this in your next extension if applicable. In this last step we will try to show some default values for fields. The repeat fields are saved as json object and if you write the default values as json object then it will work. For our 3rd step we added two fields inside the hidden field set, means in repeat box we will get two fields where one is text and another one is textarea.
Now we will see how we can put default values for two repeat box.
Now the question may come how can I get the value from the field or while I try to get the field value from Joomla params get method i.e, Example $params->get(‘myrepeatfield’) I get some json like value. Yes, you are right, just do json_decode() 😉
Hope you enjoyed the joomla native repeatable fields without using any custom library or 3rd party resource. Please note that, this features is available from jomola 3.2 and it’s recommended that you follow the latest version. Joomla now maintain one major version.
Share Joomla love if you care !