Chronoforms v7 Repeater Example

CraigH 10 Jul, 2023
I have seen several threads on this forum on the topic of using the repeater function in CF#7, but they have not helped me to get a simple form working.

I want a repeating group with several fields. I want the form to start with one repeat displayed and a button to add more if required, and remove repeats if more than one present. Using the Clone button is working fine to add/remove repeats but I am struggling to have the form start with one repeat already present.

I have set the minimum number of repeats to 1.

My tests were using no data source in the repeater. I have found that if I specify a data source, then the form does open with the required one repeat, but I cannot work out the syntax for field names to get all the repeats delivered in the data model when the form submit button is pressed.

It would be great if there was a simple example provided that shows how this is configured. I have seen mention of an example on this post:https://www.chronoengine.com/forums/topics/view/108628/explanation-of-cf7-repeater#p395579 but I cannot see anywhere where this example can be downloaded. And I don't know if this example demonstrates the simple case I am after.
CraigH 11 Jul, 2023
Through quite a bit of experimentation I have worked out how to meet the requirements I had for a simple repeater form. I will describe the dumbed down form here in case its of use for anyone else.

Requirements are:
  • To have a form with a repeating group of two fields that starts off with a single mandatory repeat displayed.
  • There needs to be a button to add additional repeats
  • If more than one repeat is displayed there should be a remove button for all repeats except the initial mandatory instance.


So created the form and added the repeater from the areas dropdown
I named the repeater area "attendee_loop"
I set the repeater data source to {data:rep}
I added the cloner advanced setting and set the minimum number to 1

I then placed the two text fields inside the repeater loop with field names "course_instance.#attendee_loop" and "attendee_name.#attendee_loop". Note the "." here in the field name.
I added a button inside the loop below the above two fields and labelled it "remove attendee" and set the button to "Button" type and added the cloner advanced setting with "Delete Clone" as the action and associated it with the "Attendee_loop" repeater.

I then added a second button outside of the repeater again with the cloner advanced setting but this time set the "Add Clone" for the "attendee_loop" repeater.

To get the form to display the mandatory first instance I created a PHP action setting the text fields to empty strings:
$this->data['rep']['1']['course_type'] = "";
$this->data['rep']['1']['attendee_name'] = "";
This causes the form to start off with the single instance already displayed. Note here you can set to values if you want to have default values or allow editing of previous multiple entries read back from the database - ie by adding entries with ['2'], ['3'] etc.

So the form starts with one repeat group displayed and the ability to add more and also remove. Once the form is submitted you can see how the form data is stored from enabling debugging:

[option] => com_chronoforms7
[view] => form
[text_1] => This is a Test
[course_type] => Array
(
[0] => Type 1
[1] => Type 2
)

[attendee_name] => Array
(
[0] => Attendee 1
[1] => Attendee 2
)
You need to login to be able to post a reply.