From 1bb49947b22065c3bce258263e2414d2453f7e03 Mon Sep 17 00:00:00 2001 From: Nathan Nguyen Date: Thu, 24 Oct 2024 09:23:37 +1100 Subject: [PATCH] Add validation for step form --- classes/local/form/form_step_instance.php | 24 +++++++++++++++++++++++ step/lib.php | 8 ++++++++ 2 files changed, 32 insertions(+) diff --git a/classes/local/form/form_step_instance.php b/classes/local/form/form_step_instance.php index 647df64c..0306e12b 100644 --- a/classes/local/form/form_step_instance.php +++ b/classes/local/form/form_step_instance.php @@ -188,4 +188,28 @@ public function definition_after_data() { } } + /** + * Validate the form. + * + * @param array $data array of ("fieldname"=>value) of submitted data + * @param array $files array of uploaded files "element_name"=>tmp_file_path + * @return array of "element_name"=>"error_description" if there are errors, + * or an empty array if everything is OK (true allowed for backwards compatibility too). + * @throws \coding_exception + */ + public function validation($data, $files) { + // Default form validation. + $error = parent::validation($data, $files); + + // Required instance name for tool_lifecycle_step table. + if (empty($data['instancename'])) { + $error['instancename'] = get_string('required'); + } + + // Allow the subplugin to add its own validation. + $this->lib->extend_add_instance_form_validation($error, $data); + + return $error; + } + } diff --git a/step/lib.php b/step/lib.php index 224399b6..6c439f9d 100644 --- a/step/lib.php +++ b/step/lib.php @@ -134,6 +134,14 @@ public function extend_add_instance_form_definition($mform) { public function extend_add_instance_form_definition_after_data($mform, $settings) { } + /** + * This method can be overriden, to add additional data validation to the instance form. + * @param array $error Array containing all errors. + * @param array $data Data passed from the moodle form to be validated + */ + public function extend_add_instance_form_validation(&$error, $data) { + } + /** * This method can be overridden. It is called when a course and the * corresponding process get deleted.