MDL-57273 form: Persistent form automatically setType on some fields

This commit is contained in:
Frederic Massart
2017-01-20 12:56:19 +08:00
committed by Damyon Wiese
parent b505a9e4e0
commit 547801a411
3 changed files with 61 additions and 0 deletions
+39
View File
@@ -134,6 +134,45 @@ abstract class persistent extends moodleform {
return $data;
}
/**
* After definition hook.
*
* Automatically try to set the types of simple fields using the persistent properties definition.
* This only applies to hidden, text and url types. Groups are also ignored as they are most likely custom.
*
* @return void
*/
protected function after_definition() {
parent::after_definition();
$mform = $this->_form;
$class = static::$persistentclass;
$properties = $class::properties_definition();
foreach ($mform->_elements as $element) {
$name = $element->getName();
if (isset($mform->_types[$name])) {
// We already have a PARAM_* type for this field.
continue;
} else if (!isset($properties[$name]) || in_array($name, static::$fieldstoremove)
|| in_array($name, static::$foreignfields)) {
// Ignoring foreign and unknown fields.
continue;
}
// Set the type on the element.
switch ($element->getType()) {
case 'hidden':
case 'text':
case 'url':
$mform->setType($name, $properties[$name]['type']);
break;
}
}
}
/**
* Define extra validation mechanims.
*