Skip to content

Commit

Permalink
issue #8: add extra form validation
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed Sep 22, 2024
1 parent 14ce980 commit adbcba6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
14 changes: 14 additions & 0 deletions classes/preset_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ protected function definition() {
$this->_form->addRule('name', get_string('required'), 'required');
$this->_form->setType('name', PARAM_TEXT);

$this->_form->addElement('static', 'error', '');

foreach ($this->get_field_types() as $type) {
$methodname = 'get_' . $type . '_options';
$this->_form->addElement(
Expand Down Expand Up @@ -119,6 +121,18 @@ protected function get_tags_options(): array {
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);

$empty = 0;
foreach ($this->get_field_types() as $type) {
if (empty($data[$type])) {
$empty++;
}
}

if ($empty == count($this->get_field_types())) {
$errors['error'] = get_string('mustselectentities', 'tool_enrolprofile');
}

return $errors;
}

Expand Down
1 change: 1 addition & 0 deletions lang/en/tool_enrolprofile.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@
$string['fieldtype:categories'] = 'Categories';
$string['fieldtype:courses'] = 'Courses';
$string['fieldtype:tags'] = 'Tags';
$string['mustselectentities'] = 'You must select at least one of the following entities';

0 comments on commit adbcba6

Please sign in to comment.