lib/formslib.php repeat elements
Advanced Usage
- repeat_elements
- setAdvanced
- no_submit_button_pressed
- add_checkbox_controller
This function adds a repeating group of elements. An array index starting with a zero index will be added to the elementname so that the form will return arrays of submitted values.
Also on the form there will be a button to add extra elements to the form. The form page reloads with extra form elements. This involves no javascript.
Contents |
Overview
Most of the necessary information is in the phpdoc comment for the repeat_elements() method:
/**
* Method to add a repeating group of elements to a form.
*
* @param array $elementobjs Array of elements or groups of elements that are to be repeated
* @param integer $repeats no of times to repeat elements initially
* @param array $options Array of options to apply to elements. Array keys are element names.
* This is an array of arrays. The second sets of keys are the option types
* for the elements :
* 'default' - default value is value
* 'helpbutton' - helpbutton params array is value
* 'disabledif' - last three moodleform::disabledIf()
* params are value as an array
* 'rule' - array of arguments for addRule for the elements
* or a string with a rule name which will be a rule name
* which be validated client side and which has a default error
* message.
* @param string $repeathiddenname name for hidden element storing no of repeats in this form
* @param string $addfieldsname name for button to add more fields
* @param int $addfieldsno how many fields to add at a time
* @param string $addstring the label for the add more fields button.
*/
The elements to repeat
Are in the $elementobjs array, with any options passed in the $options array.
WIthin these fields, for example, within the element labels or the Defaults (since Moodle 2.3) you can use {no} to represent which element number this is, counting from 1.
The number of repeats
$repeats is the number of repeats to show initially. $addfieldsno is the number of elements to add whenever the user clicks the 'add more fields' button. $addstring is the label for this button. It can use the {no} placeholder, this time for saying how many repeats will be added. $repeathiddenname is the name to use for the hidden form field that records how many repeats there are.
Examples
Here is some code using repeat_elements() from inside definition() method of the add or update a Choice form:
//-------------------------------------------------------------------------------
$repeatarray=array();
$repeatarray[] = &MoodleQuickForm::createElement('header', '', get_string('choice','choice').' {no}');
$repeatarray[] = &MoodleQuickForm::createElement('text', 'option', get_string('choice','choice'));
$repeatarray[] = &MoodleQuickForm::createElement('text', 'limit', get_string('limit','choice'));
$repeatarray[] = &MoodleQuickForm::createElement('hidden', 'optionid', 0);
if ($this->_instance){
$repeatno=count_records('choice_options', 'choiceid', $this->_instance);
$repeatno += 2;
} else {
$repeatno = 5;
}
$repeateloptions = array();
$repeateloptions['limit']['default'] = 0;
$repeateloptions['limit']['disabledif'] = array('limitanswers', 'eq', 0);
$mform->setType('limit', PARAM_INT);
$repeateloptions['option']['helpbutton'] = array('options', get_string('modulenameplural', 'choice'), 'choice');
$mform->setType('option', PARAM_TEXT);
$mform->setType('optionid', PARAM_INT);
$this->repeat_elements($repeatarray, $repeatno,
$repeateloptions, 'option_repeats', 'option_add_fields', 3);
For other examples, have a look at the question type editing forms. They may extensive use of repeat_elements().

