Make sure we always checks for failed validation, before redirecting.
A positive return value does not signal that all settings were able
to be written, only that at least one was - errors still need to be
checked!
$errormsg = '';
if ($data = data_submitted() and confirm_sesskey()) {
- if (admin_write_settings($data)) {
- $statusmsg = get_string('changessaved');
- }
-
+ $count = admin_write_settings($data);
if (empty($adminroot->errors)) {
- switch ($return) {
- case 'site': redirect("$CFG->wwwroot/");
- case 'admin': redirect("$CFG->wwwroot/$CFG->admin/");
+ // No errors. Did we change any setting? If so, then indicate success.
+ if ($count) {
+ $statusmsg = get_string('changessaved');
+ } else {
+ switch ($return) {
+ case 'site': redirect("$CFG->wwwroot/");
+ case 'admin': redirect("$CFG->wwwroot/$CFG->admin/");
+ }
}
} else {
$errormsg = get_string('errorwithsettings', 'admin');
// now we'll deal with the case that the admin has submitted the form with changed settings
if ($data = data_submitted() and confirm_sesskey() and isset($data->action) and $data->action == 'save-settings') {
require_capability('moodle/site:config', $context);
- if (admin_write_settings($data)) {
- redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
- }
-
+ $count = admin_write_settings($data);
if (!empty($adminroot->errors)) {
$errormsg = get_string('errorwithsettings', 'admin');
$firsterror = reset($adminroot->errors);
$focus = $firsterror->id;
} else {
+ // No errors. Did we change any setting? If so, then redirect with success.
+ if ($count) {
+ redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
+ }
redirect($PAGE->url);
}
}
$errormsg = '';
if ($data = data_submitted() and confirm_sesskey()) {
- if (admin_write_settings($data)) {
- redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
- }
+ $count = admin_write_settings($data);
+ // Regardless of whether any setting change was written (a positive count), check validation errors for those that didn't.
if (empty($adminroot->errors)) {
+ // No errors. Did we change any setting? If so, then redirect with success.
+ if ($count) {
+ redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
+ }
+ // We didn't change a setting.
switch ($return) {
case 'site': redirect("$CFG->wwwroot/");
case 'admin': redirect("$CFG->wwwroot/$CFG->admin/");