Development:lib/formslib.php repeat elements: Difference between revisions
Jamie Pratt (talk | contribs) (adding template to page) |
Jamie Pratt (talk | contribs) No edit summary |
||
Line 53: | Line 53: | ||
'option_repeats', 'option_add_fields', 3); | 'option_repeats', 'option_add_fields', 3); | ||
</pre> | </pre> | ||
[[Category:Formslib]] |
Revision as of 13:59, 1 December 2006
Template:Formslib 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.
From the phpdoc comments from this moodleform 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 * 'type' - PARAM_* constant is value * 'helpbutton' - helpbutton params array is value * 'disabledif' - last three moodleform::disabledIf() * params are value as an array * @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 array $addstring array of params for get_string for name of button, $a is no of fields that * will be added. */
This method can be called from within definition(). Here is an example from within chat_mod_form in chat/mod_form.php. You can see this at work if you add a chat form or update a chat form in your course.
$repeatarray=array(); $repeatarray[] = &MoodleQuickForm::createElement('header', '', get_string('choice','choice')); $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; } $this->repeat_elements($repeatarray, $repeatno, array('limit'=> array('default'=>0, 'type'=>PARAM_INT, 'disabledif'=>array('limitanswers', 'eq', 0)), 'option' => array('type'=>PARAM_TEXT, 'helpbutton'=>array('options', get_string('modulenameplural', 'choice'), 'choice')), 'optionid' => array('type'=>PARAM_INT)), 'option_repeats', 'option_add_fields', 3);