Permalink
Browse files

MDL-39650 form: Type can be set on repeated elements in a group

  • Loading branch information...
1 parent f0d8c53 commit e4b771cf0da36f2a17757d00b0e0228d62a86fb4 @FMCorz FMCorz committed May 10, 2013
Showing with 78 additions and 5 deletions.
  1. +3 −5 lib/formslib.php
  2. +75 −0 lib/tests/formslib_test.php
View
8 lib/formslib.php
@@ -1079,11 +1079,9 @@ function repeat_elements($elementobjs, $repeats, $options, $repeathiddenname,
$params = array_merge(array($realelementname), $params);
call_user_func_array(array(&$mform, 'addRule'), $params);
break;
- case 'type' :
- //Type should be set only once
- if (!isset($mform->_types[$elementname])) {
- $mform->setType($elementname, $params);
- }
+
+ case 'type':
+ $mform->setType($realelementname, $params);
break;
}
}
View
75 lib/tests/formslib_test.php
@@ -226,6 +226,24 @@ public function test_type_cleaning() {
'xyz' => PARAM_RAW
),
1 => PARAM_INT
+ ),
+ 'repeatgroupel1' => array(
+ 0 => PARAM_INT,
+ 1 => PARAM_INT
+ ),
+ 'repeatgroupel2' => array(
+ 0 => PARAM_INT,
+ 1 => PARAM_INT
+ ),
+ 'repeatnamedgroup' => array(
+ 0 => array(
+ 'repeatnamedgroupel1' => PARAM_INT,
+ 'repeatnamedgroupel2' => PARAM_INT
+ ),
+ 1 => array(
+ 'repeatnamedgroupel1' => PARAM_INT,
+ 'repeatnamedgroupel2' => PARAM_INT
+ )
)
);
$valuessubmitted = array(
@@ -262,6 +280,24 @@ public function test_type_cleaning() {
'xyz' => '11.01'
),
1 => '11.01'
+ ),
+ 'repeatgroupel1' => array(
+ 0 => '11.01',
+ 1 => '11.01'
+ ),
+ 'repeatgroupel2' => array(
+ 0 => '11.01',
+ 1 => '11.01'
+ ),
+ 'repeatnamedgroup' => array(
+ 0 => array(
+ 'repeatnamedgroupel1' => '11.01',
+ 'repeatnamedgroupel2' => '11.01'
+ ),
+ 1 => array(
+ 'repeatnamedgroupel1' => '11.01',
+ 'repeatnamedgroupel2' => '11.01'
+ )
)
);
$expectedvalues = array(
@@ -300,6 +336,26 @@ public function test_type_cleaning() {
'xyz' => '11.01'
),
1 => 11
+ ),
+ 'repeatablegroup' => 2,
+ 'repeatgroupel1' => array(
+ 0 => 11,
+ 1 => 11
+ ),
+ 'repeatgroupel2' => array(
+ 0 => 11,
+ 1 => 11
+ ),
+ 'repeatablenamedgroup' => 2,
+ 'repeatnamedgroup' => array(
+ 0 => array(
+ 'repeatnamedgroupel1' => 11,
+ 'repeatnamedgroupel2' => 11
+ ),
+ 1 => array(
+ 'repeatnamedgroupel1' => 11,
+ 'repeatnamedgroupel2' => 11
+ )
)
);
@@ -342,6 +398,7 @@ public function definition() {
}
}
+// Used to check value cleaning.
class formslib_clean_value extends moodleform {
public function get_form() {
return $this->_form;
@@ -407,5 +464,23 @@ public function definition() {
$mform->addElement('text', 'nested[1]', 'nested[1]');
$mform->addElement('text', 'nested[0][xyz]', 'nested[0][xyz]');
$mform->addElement('text', 'nested[0][bob][foo]', 'nested[0][bob][foo]');
+
+ // Add group in repeated element (with extra inheritance).
+ $groupelements = array(
+ $mform->createElement('text', 'repeatgroupel1', 'repeatgroupel1'),
+ $mform->createElement('text', 'repeatgroupel2', 'repeatgroupel2')
+ );
+ $group = $mform->createElement('group', 'repeatgroup', 'repeatgroup', $groupelements, null, false);
+ $this->repeat_elements(array($group), 2, array('repeatgroupel1' => array('type' => PARAM_INT),
+ 'repeatgroupel2' => array('type' => PARAM_INT)), 'repeatablegroup', 'add', 0);
+
+ // Add named group in repeated element.
+ $groupelements = array(
+ $mform->createElement('text', 'repeatnamedgroupel1', 'repeatnamedgroupel1'),
+ $mform->createElement('text', 'repeatnamedgroupel2', 'repeatnamedgroupel2')
+ );
+ $group = $mform->createElement('group', 'repeatnamedgroup', 'repeatnamedgroup', $groupelements, null, true);
+ $this->repeat_elements(array($group), 2, array('repeatnamedgroup[repeatnamedgroupel1]' => array('type' => PARAM_INT),
+ 'repeatnamedgroup[repeatnamedgroupel2]' => array('type' => PARAM_INT)), 'repeatablenamedgroup', 'add', 0);
}
}

0 comments on commit e4b771c

Please sign in to comment.