MDL-53140 forms: Implement validateSubmitValue for the group element
[moodle.git] / lib / form / group.php
CommitLineData
da6f8763 1<?php
6c1fd304
RT
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17
18/**
19 * Form element group
20 *
21 * Contains HTML class for group form element
22 *
23 * @package core_form
24 * @copyright 2007 Jamie Pratt <me@jamiep.org>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
da6f8763 28require_once("HTML/QuickForm/group.php");
344321e1 29require_once('templatable_form_element.php');
da6f8763 30
31/**
32 * HTML class for a form element group
d4fe14d3 33 *
58b7d48f 34 * Overloaded {@link HTML_QuickForm_group} with default behavior modified for Moodle.
6c1fd304
RT
35 *
36 * @package core_form
37 * @category form
38 * @copyright 2007 Jamie Pratt <me@jamiep.org>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
da6f8763 40 */
91bda4cd 41class MoodleQuickForm_group extends HTML_QuickForm_group implements templatable {
344321e1
DW
42 use templatable_form_element {
43 export_for_template as export_for_template_base;
44 }
45
6c1fd304
RT
46 /** @var string html for help button, if empty then no help */
47 var $_helpbutton='';
48
721e2def
MG
49 /** @var MoodleQuickForm */
50 protected $_mform = null;
51
e584e6ae 52 protected $_renderedfromtemplate = false;
91bda4cd 53
da6f8763 54 /**
6c1fd304 55 * constructor
da6f8763 56 *
6c1fd304
RT
57 * @param string $elementName (optional) name of the group
58 * @param string $elementLabel (optional) group label
59 * @param array $elements (optional) array of HTML_QuickForm_element elements to group
60 * @param string $separator (optional) string to seperate elements.
61 * @param string $appendName (optional) string to appened to grouped elements.
da6f8763 62 */
1a0df553
MG
63 public function __construct($elementName=null, $elementLabel=null, $elements=null, $separator=null, $appendName = true) {
64 parent::__construct($elementName, $elementLabel, $elements, $separator, $appendName);
65 }
66
67 /**
68 * Old syntax of class constructor. Deprecated in PHP7.
69 *
70 * @deprecated since Moodle 3.1
71 */
72 public function MoodleQuickForm_group($elementName=null, $elementLabel=null, $elements=null, $separator=null, $appendName = true) {
73 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
74 self::__construct($elementName, $elementLabel, $elements, $separator, $appendName);
3e8b394e 75 }
6c1fd304
RT
76
77 /** @var string template type, would cause problems with client side validation so will leave for now */
ef625b6b 78 //var $_elementTemplateType='fieldset';
6c1fd304 79
da6f8763 80 /**
d4fe14d3 81 * set html for help button
da6f8763 82 */
83 function getHelpButton(){
84 return $this->_helpbutton;
85 }
6c1fd304
RT
86
87 /**
88 * Returns element template, nodisplay/static/fieldset
89 *
90 * @return string
91 */
ef625b6b 92 function getElementTemplateType(){
64360ee7 93 if ($this->_flagFrozen){
94 if ($this->getGroupType() == 'submit'){
95 return 'nodisplay';
96 } else {
97 return 'static';
98 }
99 } else {
95114b18
FM
100 if ($this->getGroupType() == 'submit') {
101 return 'actionbuttons';
102 }
64360ee7 103 return 'fieldset';
104 }
ef625b6b 105 }
64360ee7 106
6c1fd304
RT
107 /**
108 * Sets the grouped elements and hides label
109 *
110 * @param array $elements
111 */
44875d78 112 function setElements($elements){
113 parent::setElements($elements);
114 foreach ($this->_elements as $element){
115 if (method_exists($element, 'setHiddenLabel')){
116 $element->setHiddenLabel(true);
117 }
118 }
119 }
721e2def
MG
120
121 /**
122 * Stores the form this element was added to
123 * This object is later used by {@link MoodleQuickForm_group::createElement()}
124 * @param null|MoodleQuickForm $mform
125 */
126 public function setMoodleForm($mform) {
127 if ($mform && $mform instanceof MoodleQuickForm) {
128 $this->_mform = $mform;
129 }
130 }
131
132 /**
133 * Called by HTML_QuickForm whenever form event is made on this element
134 *
135 * If this function is overridden and parent is not called the element must be responsible for
136 * storing the MoodleQuickForm object, see {@link MoodleQuickForm_group::setMoodleForm()}
137 *
138 * @param string $event Name of event
139 * @param mixed $arg event arguments
140 * @param mixed $caller calling object
141 */
142 public function onQuickFormEvent($event, $arg, &$caller) {
143 $this->setMoodleForm($caller);
144 return parent::onQuickFormEvent($event, $arg, $caller);
145 }
146
147 /**
148 * Creates an element to add to the group
149 * Expects the same arguments as MoodleQuickForm::createElement()
150 */
151 public function createFormElement() {
152 if (!$this->_mform) {
153 throw new coding_exception('You can not call createFormElement() on the group element that was not yet added to a form.');
154 }
155 return call_user_func_array([$this->_mform, 'createElement'], func_get_args());
156 }
91bda4cd
DW
157
158 public function export_for_template(renderer_base $output) {
159 global $OUTPUT;
344321e1
DW
160
161 $context = $this->export_for_template_base($output);
162
91bda4cd
DW
163 $this->_renderedfromtemplate = true;
164
165 include_once('HTML/QuickForm/Renderer/Default.php');
166
91bda4cd 167 $elements = [];
63e4df60 168 $name = $this->getName();
b2421251 169 $i = 0;
91bda4cd 170 foreach ($this->_elements as $key => $element) {
91bda4cd
DW
171 $elementname = '';
172 if ($this->_appendName) {
173 $elementname = $element->getName();
174 if (isset($elementname)) {
175 $element->setName($name . '['. (strlen($elementname) ? $elementname : $key) .']');
176 } else {
177 $element->setName($name);
178 }
179 }
63e4df60 180 $element->_generateId();
91bda4cd 181
d6f2ce14 182 $out = $OUTPUT->mform_element($element, false, false, '', true);
91bda4cd
DW
183
184 if (empty($out)) {
185 $renderer = new HTML_QuickForm_Renderer_Default();
186 $renderer->setElementTemplate('{element}');
187 $element->accept($renderer);
188 $out = $renderer->toHtml();
189 }
b2421251
FM
190
191 // Replicates the separator logic from 'pear/HTML/QuickForm/Renderer/Default.php'.
192 $separator = '';
193 if ($i > 0) {
194 if (is_array($this->_separator)) {
195 $separator = $this->_separator[($i - 1) % count($this->_separator)];
196 } else if ($this->_separator === null) {
197 $separator = '&nbsp;';
198 } else {
199 $separator = (string) $this->_separator;
200 }
201 }
202
203 $elements[] = [
204 'separator' => $separator,
205 'html' => $out
206 ];
207
91bda4cd
DW
208 // Restore the element's name.
209 if ($this->_appendName) {
210 $element->setName($elementname);
211 }
212
b2421251 213 $i++;
91bda4cd
DW
214 }
215
766d6f9a 216 $context['groupname'] = $name;
91bda4cd
DW
217 $context['elements'] = $elements;
218 return $context;
219 }
220
e584e6ae
DW
221 /**
222 * Accepts a renderer
223 *
224 * @param object An HTML_QuickForm_Renderer object
225 * @param bool Whether a group is required
226 * @param string An error message associated with a group
227 * @access public
228 * @return void
229 */
230 public function accept(&$renderer, $required = false, $error = null) {
91bda4cd
DW
231 $this->_createElementsIfNotExist();
232 $renderer->startGroup($this, $required, $error);
233 if (!$this->_renderedfromtemplate) {
234 // Backwards compatible path - only do this if we didn't render the sub-elements already.
235 $name = $this->getName();
236 foreach (array_keys($this->_elements) as $key) {
237 $element =& $this->_elements[$key];
e584e6ae 238 $elementname = '';
91bda4cd 239 if ($this->_appendName) {
e584e6ae
DW
240 $elementname = $element->getName();
241 if (isset($elementname)) {
242 $element->setName($name . '['. (strlen($elementname) ? $elementname : $key) .']');
91bda4cd
DW
243 } else {
244 $element->setName($name);
245 }
246 }
247
248 $required = !$element->isFrozen() && in_array($element->getName(), $this->_required);
249
250 $element->accept($renderer, $required);
251
e584e6ae 252 // Restore the element's name.
91bda4cd 253 if ($this->_appendName) {
e584e6ae 254 $element->setName($elementname);
91bda4cd
DW
255 }
256 }
257 }
258 $renderer->finishGroup($this);
259 }
f8c31aba
SR
260
261 /**
262 * Calls the validateSubmitValue function for the containing elements and returns an error string as soon as it finds one.
263 *
264 * @param array $values Values of the containing elements.
265 * @return string|null Validation error message or null.
266 */
267 public function validateSubmitValue($values) {
268 foreach ($this->_elements as $element) {
269 if (method_exists($element, 'validateSubmitValue')) {
270 $value = $values[$element->getName()] ?? null;
271 $result = $element->validateSubmitValue($value);
272 if (!empty($result) && is_string($result)) {
273 return $result;
274 }
275 }
276 }
277 }
e24b7f85 278}