Skip to content

Commit

Permalink
Add validation for step form
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Nguyen committed Oct 23, 2024
1 parent 8b5ad7f commit cac6eb9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
24 changes: 24 additions & 0 deletions classes/local/form/form_step_instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,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;
}

}
8 changes: 8 additions & 0 deletions step/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit cac6eb9

Please sign in to comment.