moodle/moodle · warning · invalid_parameter_exception

$format must be one of question_text_format::OFF, ::PLAIN…

Error message

$format must be one of question_text_format::OFF, ::PLAIN or ::FULL.

What it means

Thrown by public/question/bank/viewquestiontext/save.php when the user preference value submitted for the question text display format is not one of the allowed \qbank_viewquestiontext\output\question_text_format constants (OFF, PLAIN, FULL). The page validates the incoming $format parameter with in_array() against an explicit allow-list and throws \invalid_parameter_exception before persisting it via question_set_or_get_user_preference('qbshowtext', ...). It is a strict enum validation on an external request parameter.

Solutions

  1. Inspect the request and confirm the $format parameter value; map it to a valid question_text_format constant (OFF, PLAIN, or FULL).
  2. In the calling UI, generate the value from \qbank_viewquestiontext\output\question_text_format::* constants instead of hard-coded numbers.
  3. Purge caches / hard-reload so the renderer emits current format values.
  4. If migrating stored preferences, clean up legacy qbshowtext user_preference rows with unsupported values via question_set_or_get_user_preference.

Example fix

// before
$format = required_param('format', PARAM_INT);
redirect(new moodle_url($returnurl));
// after
$format = required_param('format', PARAM_INT);
$validformats = [
    \qbank_viewquestiontext\output\question_text_format::OFF,
    \qbank_viewquestiontext\output\question_text_format::PLAIN,
    \qbank_viewquestiontext\output\question_text_format::FULL,
];
if (!in_array($format, $validformats)) {
    $format = \qbank_viewquestiontext\output\question_text_format::OFF;
}
question_set_or_get_user_preference('qbshowtext', $format, 0, new \moodle_url('/'));
Defensive patterns

Strategy: validation

Validate before calling

$validformats = [\qbank_viewquestiontext\output\question_text_format::OFF, \qbank_viewquestiontext\output\question_text_format::PLAIN, \qbank_viewquestiontext\output\question_text_format::FULL];
if (!in_array($format, $validformats, true)) {
    $format = \qbank_viewquestiontext\output\question_text_format::OFF;
}

Type guard

function is_valid_question_text_format($value): bool {
    return in_array($value, [\qbank_viewquestiontext\output\question_text_format::OFF, \qbank_viewquestiontext\output\question_text_format::PLAIN, \qbank_viewquestiontext\output\question_text_format::FULL], true);
}

Try / catch

try {
    question_set_or_get_user_preference('qbshowtext', $format, 0, new \moodle_url('/'));
} catch (\invalid_parameter_exception $e) {
    debugging('Invalid qbshowtext format supplied, defaulting to OFF', DEBUG_DEVELOPER);
    question_set_or_get_user_preference('qbshowtext', \qbank_viewquestiontext\output\question_text_format::OFF, 0, new \moodle_url('/'));
}

Prevention

When it happens

Trigger: Calling save.php with a $format value that is not OFF, PLAIN or FULL — e.g. an integer outside the constant values, an empty string, a tampered or stale URL parameter, or a client submitting the old numeric preference convention after the format class was introduced.

Common situations: Hand-crafted or bookmarked links to save.php with an outdated qbshowtext value; third-party code that wrote the user preference directly using an unsupported value; upgrades of qbank_viewquestiontext where the constant set changed but cached UI markup still posts a legacy value.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14). Data as JSON: /api/errors/650e87aad7bc409f. Report an issue: GitHub.

Appendix: source

Thrown at public/question/bank/viewquestiontext/save.php:43

 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

require_once(__DIR__ . '/../../../config.php');
require_once($CFG->dirroot . '/question/editlib.php');

$format = required_param('format', PARAM_INT);
$returnurl = required_param('returnurl', PARAM_LOCALURL);

require_login();
require_sesskey();

$validformats = [
    \qbank_viewquestiontext\output\question_text_format::OFF,
    \qbank_viewquestiontext\output\question_text_format::PLAIN,
    \qbank_viewquestiontext\output\question_text_format::FULL,
];
if (!in_array($format, $validformats)) {
    throw new \invalid_parameter_exception('$format must be one of question_text_format::OFF, ::PLAIN or ::FULL.');
}

question_set_or_get_user_preference('qbshowtext', $format, 0, new \moodle_url('/'));

redirect(new moodle_url($returnurl));

View on GitHub (pinned to 48ea8c3322)