Skip to content

Commit

Permalink
MDL-51250 course: Add validation for section name
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Oct 7, 2015
1 parent 08a5417 commit d264cbb
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions course/editsection_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ function definition() {
$mform->addGroup($elementgroup, 'name_group', get_string('sectionname'), ' ', false);
$mform->addGroupRule('name_group', array('name' => array(array(get_string('maximumchars', '', 255), 'maxlength', 255))));

// Add rule for name_group to make sure that the section name is not blank if 'Use default section name'
// checkbox is unchecked.
$mform->addRule('name_group', get_string('required'), 'required', null, 'client');

$mform->setDefault('usedefaultname', true);
$mform->setType('name', PARAM_TEXT);
$mform->disabledIf('name','usedefaultname','checked');
Expand Down Expand Up @@ -112,8 +116,7 @@ function get_data() {
$data = parent::get_data();
if ($data !== null) {
$editoroptions = $this->_customdata['editoroptions'];
$trimmedname = $data->name;
if (!empty($data->usedefaultname) || empty($trimmedname)) {
if (!empty($data->usedefaultname)) {
$data->name = null;
}
$data = file_postupdate_standard_editor($data, 'summary', $editoroptions,
Expand All @@ -138,6 +141,15 @@ public function validation($data, $files) {
\core_availability\frontend::report_validation_errors($data, $errors);
}

// Validate section name if 'Use default section name' is unchecked.
if (empty($data['usedefaultname'])) {
// Make sure the trimmed value of section name is not empty.
$trimmedname = trim($data['name']);
if (empty($trimmedname)) {
$errors['name_group'] = get_string('required');
}
}

return $errors;
}
}

0 comments on commit d264cbb

Please sign in to comment.