Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 39 and 401]

Persistent form abstract.

Copyright: 2015 Frédéric Massart - FMCorz.net
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 320 lines (11 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: persistent  - X-Ref

Persistent form abstract class.

This provides some shortcuts to validate objects based on the persistent model.

Note that all mandatory fields (non-optional) of your model should be included in the
form definition. Mandatory fields which are not editable by the user should be
as hidden and constant.

$mform->addElement('hidden', 'userid');
$mform->setType('userid', PARAM_INT);
$mform->setConstant('userid', $this->_customdata['userid']);

You may exclude some fields from the validation should your form include other
properties such as files. To do so use the $foreignfields property.

__construct($action = null, $customdata = null, $method = 'post', $target = '',$attributes = null, $editable = true, $ajaxformdata=null)   X-Ref
Constructor.

The 'persistent' has to be passed as custom data when 'editing'.

Note that in order for your persistent to be reloaded after form submission you should
either override the URL to include the ID to your resource, or add the ID to the form
fields.

param: mixed $action
param: mixed $customdata
param: string $method
param: string $target
param: mixed $attributes
param: bool $editable
param: array $ajaxformdata

convert_fields(stdClass $data)   X-Ref
Convert some fields.

param: stdClass $data The whole data set.
return: stdClass The amended data set.

after_definition()   X-Ref
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

extra_validation($data, $files, array &$errors)   X-Ref
Define extra validation mechanims.

The data here:
- does not include {@link self::$fieldstoremove}.
- does include {@link self::$foreignfields}.
- was converted to map persistent-like data, e.g. array $description to string $description + int $descriptionformat.

You can modify the $errors parameter in order to remove some validation errors should you
need to. However, the best practice is to return new or overriden errors. Only modify the
errors passed by reference when you have no other option.

Do not add any logic here, it is only intended to be used by child classes.

param: stdClass $data Data to validate.
param: array $files Array of files.
param: array $errors Currently reported errors.
return: array of additional errors, or overridden errors.

filter_data_for_persistent($data)   X-Ref
Filter out the foreign fields of the persistent.

This can be overridden to filter out more complex fields.

param: stdClass $data The data to filter the fields out of.
return: stdClass.

get_default_data()   X-Ref
Get the default data.

This is the data that is prepopulated in the form at it loads, we automatically
fetch all the properties of the persistent however some needs to be converted
to map the form structure.

Extend this class if you need to add more conversion.

return: stdClass

get_data()   X-Ref
Get form data.

Conveniently removes non-desired properties and add the ID property.

return: object|null

get_persistent()   X-Ref
Return the persistent object associated with this form instance.

return: \core\persistent

get_submitted_data()   X-Ref
Get the submitted form data.

Conveniently removes non-desired properties.

return: object|null

validation($data, $files)   X-Ref
Form validation.

If you need extra validation, use {@link self::extra_validation()}.

param: array $data
param: array $files
return: array