## General developer forum

### Repeat group of elements (in form)

Repeat group of elements (in form)

Hi everyone,

I'm writting a plugin and in that plugin I need a form that lets user choose parameters, and then generates a graph depending on those parameters.

I had a simple form (choose one activity) and a double form (choose two activities).
I'm trying now to merge these two forms into one form only.

I used the repeat_elements(); function, to repeat as many times wanted the group of elements.
https://docs.moodle.org/dev/lib/formslib.php_repeat_elements

But get some problems :

1. The first group of elements I declare before repeating is showed. So I have "id_activitygroup_activity" and then only I have my repeated elements : "id_activitygroup_0_activity", "id_activitygroup_1_activity", etc.
Could it be possible to not show first element declared ?
2. The repeating element group has a setting to set the max ammount of repetitions but I can click as many times on "add field" button, and I get new fields in form.
3. I used without any problem "disableIF" before, to manually disable all fields i wanted if one select was not choosed. (eg. If you don't want to calculate average, all boxes to set average are disabled). I'm trying now to disable them even if we add new (disable all instances) but can't manage to do that.

Has anyone an idea about where to start ?
My code is here : https://pastebin.com/P2pbbwDc (full code in attachment).

Thank you very much !!

Average of ratings: -
Re: Repeat group of elements (in form)

Ok so here some news, after hours trying, I found some of problems :

1. To not let the group appear once before repeating, or when you want to create a group that will be put in array for repeat, you should use createElement instead of addGroup.

2. Still didn't find how to do it. I can get number of repetitions actually showed by doing :

$this->_form->_elements[9]->_attributes['value'] How to make "add fields" button disable when value is something ? 3. Managed to make it work, don't know what was the problem. I think I was using two ways to set it, replaced with only the good one (for repeaters, in options, like that $repeatoptions['my_field']['disabledif'] = array('field_to_check', 'eq', 0);

Thank you very much for reading me.

If anyone has an idea for the problem #2, I'll be there ! Thanks again !

Average of ratings: -