MDL-57273 form: Persistent form automatically setType on some fields
This commit is contained in:
committed by
Damyon Wiese
parent
b505a9e4e0
commit
547801a411
@@ -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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user