From 0645319e623e63491dacf4bde8f06a5ae405197f Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Tue, 10 Aug 2021 19:17:31 +0100 Subject: [PATCH] MDL-72309 course: make category a required field in forms. Course category fields were changed en masse from simple select elements to autocompletes in 93d72205, but weren't defined as required fields. This could lead to exceptions and/or unexpected behaviour if their values were cleared prior to form submission. --- admin/tool/uploadcourse/classes/step2_form.php | 1 + backup/util/ui/classes/output/copy_form.php | 1 + cohort/edit_form.php | 1 + course/classes/deletecategory_form.php | 1 + course/classes/editcategory_form.php | 1 + course/edit_form.php | 2 ++ course/request_form.php | 1 + 7 files changed, 8 insertions(+) diff --git a/admin/tool/uploadcourse/classes/step2_form.php b/admin/tool/uploadcourse/classes/step2_form.php index a6875f68263..4cd4f27c2be 100644 --- a/admin/tool/uploadcourse/classes/step2_form.php +++ b/admin/tool/uploadcourse/classes/step2_form.php @@ -84,6 +84,7 @@ class tool_uploadcourse_step2_form extends tool_uploadcourse_base_form { $displaylist = core_course_category::make_categories_list('moodle/course:create'); $mform->addElement('autocomplete', 'defaults[category]', get_string('coursecategory'), $displaylist); + $mform->addRule('defaults[category]', null, 'required', null, 'client'); $mform->addHelpButton('defaults[category]', 'coursecategory'); $choices = array(); diff --git a/backup/util/ui/classes/output/copy_form.php b/backup/util/ui/classes/output/copy_form.php index 51f8bb8815f..78d56c796c7 100644 --- a/backup/util/ui/classes/output/copy_form.php +++ b/backup/util/ui/classes/output/copy_form.php @@ -107,6 +107,7 @@ class copy_form extends \moodleform { $displaylist[$course->category] = \core_course_category::get($course->category, MUST_EXIST, true)->get_formatted_name(); } $mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist); + $mform->addRule('category', null, 'required', null, 'client'); $mform->addHelpButton('category', 'coursecategory'); // Course visibility. diff --git a/cohort/edit_form.php b/cohort/edit_form.php index 77599fd1dc2..55e7a54284d 100644 --- a/cohort/edit_form.php +++ b/cohort/edit_form.php @@ -44,6 +44,7 @@ class cohort_edit_form extends moodleform { $options = $this->get_category_options($cohort->contextid); $mform->addElement('autocomplete', 'contextid', get_string('context', 'role'), $options); + $mform->addRule('contextid', null, 'required', null, 'client'); $mform->addElement('text', 'idnumber', get_string('idnumber', 'cohort'), 'maxlength="254" size="50"'); $mform->setType('idnumber', PARAM_RAW); // Idnumbers are plain text, must not be changed. diff --git a/course/classes/deletecategory_form.php b/course/classes/deletecategory_form.php index 906837e505d..a56661148e9 100644 --- a/course/classes/deletecategory_form.php +++ b/course/classes/deletecategory_form.php @@ -112,6 +112,7 @@ class core_course_deletecategory_form extends moodleform { if ($displaylist) { $mform->addElement('autocomplete', 'newparent', get_string('movecategorycontentto'), $displaylist); + $mform->addRule('newparent', null, 'required', null, 'client'); if (in_array($this->coursecat->parent, $displaylist)) { $mform->setDefault('newparent', $this->coursecat->parent); } diff --git a/course/classes/editcategory_form.php b/course/classes/editcategory_form.php index 236fbad0901..1006fb4e572 100644 --- a/course/classes/editcategory_form.php +++ b/course/classes/editcategory_form.php @@ -64,6 +64,7 @@ class core_course_editcategory_form extends moodleform { } $mform->addElement('autocomplete', 'parent', get_string('parentcategory'), $options); + $mform->addRule('parent', null, 'required', null, 'client'); $mform->addElement('text', 'name', get_string('categoryname'), array('size' => '30')); $mform->addRule('name', get_string('required'), 'required', null); diff --git a/course/edit_form.php b/course/edit_form.php index 73b5190c3bf..5654393df0a 100644 --- a/course/edit_form.php +++ b/course/edit_form.php @@ -78,6 +78,7 @@ class course_edit_form extends moodleform { if (has_capability('moodle/course:create', $categorycontext)) { $displaylist = core_course_category::make_categories_list('moodle/course:create'); $mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist); + $mform->addRule('category', null, 'required', null, 'client'); $mform->addHelpButton('category', 'coursecategory'); $mform->setDefault('category', $category->id); } else { @@ -94,6 +95,7 @@ class course_edit_form extends moodleform { ->get_formatted_name(); } $mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist); + $mform->addRule('category', null, 'required', null, 'client'); $mform->addHelpButton('category', 'coursecategory'); } else { //keep current diff --git a/course/request_form.php b/course/request_form.php index d9179700119..332da47852d 100644 --- a/course/request_form.php +++ b/course/request_form.php @@ -71,6 +71,7 @@ class course_request_form extends moodleform { if (empty($CFG->lockrequestcategory)) { $displaylist = core_course_category::make_categories_list('moodle/course:request'); $mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist); + $mform->addRule('category', null, 'required', null, 'client'); $mform->setDefault('category', $CFG->defaultrequestcategory); $mform->addHelpButton('category', 'coursecategory'); } -- 2.11.4.GIT