Skip to content

Commit

Permalink
MDL-30638 mod_choice: Add required elements to repeated form items
Browse files Browse the repository at this point in the history
To make it work properly with MDL-30637
  • Loading branch information
kabalin committed Feb 7, 2013
1 parent a4067bf commit 9d2a7df
Showing 1 changed file with 5 additions and 20 deletions.
25 changes: 5 additions & 20 deletions mod/choice/mod_form.php
Expand Up @@ -35,7 +35,7 @@ function definition() {
$menuoptions = array(); $menuoptions = array();
$menuoptions[0] = get_string('disable'); $menuoptions[0] = get_string('disable');
$menuoptions[1] = get_string('enable'); $menuoptions[1] = get_string('enable');
$mform->addElement('header', 'timerestricthdr', get_string('limit', 'choice')); $mform->addElement('header', 'limithdr', get_string('limit', 'choice'));
$mform->addElement('select', 'limitanswers', get_string('limitanswers', 'choice'), $menuoptions); $mform->addElement('select', 'limitanswers', get_string('limitanswers', 'choice'), $menuoptions);
$mform->addHelpButton('limitanswers', 'limitanswers', 'choice'); $mform->addHelpButton('limitanswers', 'limitanswers', 'choice');


Expand All @@ -59,8 +59,10 @@ function definition() {
$this->repeat_elements($repeatarray, $repeatno, $this->repeat_elements($repeatarray, $repeatno,
$repeateloptions, 'option_repeats', 'option_add_fields', 3); $repeateloptions, 'option_repeats', 'option_add_fields', 3);



// Make the first option required

if ($mform->elementExists('option[0]')) {
$mform->addRule('option[0]', get_string('atleastoneoption', 'choice'), 'required', null, 'client');
}


//------------------------------------------------------------------------------- //-------------------------------------------------------------------------------
$mform->addElement('header', 'timerestricthdr', get_string('timerestrict', 'choice')); $mform->addElement('header', 'timerestricthdr', get_string('timerestrict', 'choice'));
Expand Down Expand Up @@ -116,23 +118,6 @@ function data_preprocessing(&$default_values){


} }


function validation($data, $files) {
$errors = parent::validation($data, $files);

$choices = 0;
foreach ($data['option'] as $option){
if (trim($option) != ''){
$choices++;
}
}

if ($choices < 1) {
$errors['option[0]'] = get_string('atleastoneoption', 'choice');
}

return $errors;
}

function get_data() { function get_data() {
$data = parent::get_data(); $data = parent::get_data();
if (!$data) { if (!$data) {
Expand Down

0 comments on commit 9d2a7df

Please sign in to comment.