lib/formslib.php repeat elements: diferència entre les revisions
m better formatting |
m Correction of reference to choice module (previously referred to as Chat) |
||
| (3 revisions intermèdies per 2 usuaris que no es mostren) | |||
| Línia 16: | Línia 16: | ||
* for the elements : | * for the elements : | ||
* 'default' - default value is value | * 'default' - default value is value | ||
* 'helpbutton' - helpbutton params array is value | * 'helpbutton' - helpbutton params array is value | ||
* 'disabledif' - last three moodleform::disabledIf() | * 'disabledif' - last three moodleform::disabledIf() | ||
* params are value as an array | * 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 $repeathiddenname name for hidden element storing no of repeats in this form | ||
* @param string $addfieldsname name for button to add more fields | * @param string $addfieldsname name for button to add more fields | ||
| Línia 28: | Línia 31: | ||
</pre> | </pre> | ||
This method can be called from within definition(). Here is an example from within | Actually it seems that $addstring is more like this: get_string('key', 'module', '{no}') in which $a is put into by formslib somewhere. (Penny) | ||
This method can be called from within definition(). Here is an example from within choice_mod_form in choice/mod_form.php. You can see this at work if you add or update a Choice in your course. | |||
<pre> | <pre> | ||
//------------------------------------------------------------------------------- | |||
$repeatarray=array(); | $repeatarray=array(); | ||
$repeatarray[] = &MoodleQuickForm::createElement('header', '', get_string('choice','choice')); | $repeatarray[] = &MoodleQuickForm::createElement('header', '', get_string('choice','choice').' {no}'); | ||
$repeatarray[] = &MoodleQuickForm::createElement('text', 'option', 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('text', 'limit', get_string('limit','choice')); | ||
| Línia 45: | Línia 51: | ||
$repeateloptions = array(); | $repeateloptions = array(); | ||
$repeateloptions['limit'] | $repeateloptions['limit']['default'] = 0; | ||
$repeateloptions['limit']['disabledif'] = array('limitanswers', 'eq', 0); | |||
$mform->setType('limit', PARAM_INT); | |||
$repeateloptions['option'] | $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, | $this->repeat_elements($repeatarray, $repeatno, | ||
$repeateloptions, 'option_repeats', 'option_add_fields', 3); | $repeateloptions, 'option_repeats', 'option_add_fields', 3); | ||
</pre> | </pre> | ||
Note: I have experienced some weirdness when creating the elements for repeating statically, especially for textarea and htmleditor. Try $mform->createElement instead if this happens (Penny) | |||
[[Category:Formslib]] | [[Category:Formslib]] | ||
Revisió de 02:55, 21 des 2009
Plantilla: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
* '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 array $addstring array of params for get_string for name of button, $a is no of fields that
* will be added.
*/
Actually it seems that $addstring is more like this: get_string('key', 'module', '{no}') in which $a is put into by formslib somewhere. (Penny)
This method can be called from within definition(). Here is an example from within choice_mod_form in choice/mod_form.php. You can see this at work if you add or update a Choice in your course.
//-------------------------------------------------------------------------------
$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);
Note: I have experienced some weirdness when creating the elements for repeating statically, especially for textarea and htmleditor. Try $mform->createElement instead if this happens (Penny)