Best way to repeat mform elements

Best way to repeat mform elements

by Mike Grant -
Number of replies: 2

I'm working on a form at the moment, and starting to tie myself up in knots using the forms.

I started by using foreach to go through the existing data and create inputs related to it, which works great for maintaining what is there. Each field is tied back to the id of the record e.g. editor[1], editor[5], editor[27].

But I want to be able to add extra form fields, and found $mform->repeated_elements. But this wants to create everything using a sequential id which isn't what I want at all. 

So, does anyone know the best way to repeat mform elements? There are 2 fields that are needed per record, which are sectionname and content.

Thanks

Average of ratings: -
In reply to Mike Grant

Re: Best way to repeat mform elements

by lior gil -
Picture of Core developers

Don't give up on repeat_elements. The sequential id is just for the elements themselves, and there's an array that helps set the appropriate element to its source.

You can find an example in your code at /mod/choice/mod_form.php.

Look at the data_preprocessing function to learn how to set the values.

Average of ratings: Useful (1)
In reply to lior gil

Re: Best way to repeat mform elements

by Mike Grant -

Thank you very much for this, exactly what I needed! I don't know how I missed it, but it's obvious now you've pointed it out to me.

Thank you again!