{"record":{"id":"650e87aad7bc409f","repo":"moodle/moodle","slug":"format-must-be-one-of-question-text-format-off-plain-or-full-650e87","errorCode":null,"errorMessage":"$format must be one of question_text_format::OFF, ::PLAIN or ::FULL.","messagePattern":"\\$format must be one of question_text_format::OFF, ::PLAIN or ::FULL\\.","errorType":"validation","errorClass":"invalid_parameter_exception","httpStatus":null,"severity":"warning","filePath":"public/question/bank/viewquestiontext/save.php","lineNumber":43,"sourceCode":" * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nrequire_once(__DIR__ . '/../../../config.php');\nrequire_once($CFG->dirroot . '/question/editlib.php');\n\n$format = required_param('format', PARAM_INT);\n$returnurl = required_param('returnurl', PARAM_LOCALURL);\n\nrequire_login();\nrequire_sesskey();\n\n$validformats = [\n    \\qbank_viewquestiontext\\output\\question_text_format::OFF,\n    \\qbank_viewquestiontext\\output\\question_text_format::PLAIN,\n    \\qbank_viewquestiontext\\output\\question_text_format::FULL,\n];\nif (!in_array($format, $validformats)) {\n    throw new \\invalid_parameter_exception('$format must be one of question_text_format::OFF, ::PLAIN or ::FULL.');\n}\n\nquestion_set_or_get_user_preference('qbshowtext', $format, 0, new \\moodle_url('/'));\n\nredirect(new moodle_url($returnurl));\n","sourceCodeStart":25,"sourceCodeEnd":49,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/question/bank/viewquestiontext/save.php#L25-L49","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Inspect the request and confirm the $format parameter value; map it to a valid question_text_format constant (OFF, PLAIN, or FULL).","In the calling UI, generate the value from \\qbank_viewquestiontext\\output\\question_text_format::* constants instead of hard-coded numbers.","Purge caches / hard-reload so the renderer emits current format values.","If migrating stored preferences, clean up legacy qbshowtext user_preference rows with unsupported values via question_set_or_get_user_preference."],"exampleFix":"// before\n$format = required_param('format', PARAM_INT);\nredirect(new moodle_url($returnurl));\n// after\n$format = required_param('format', PARAM_INT);\n$validformats = [\n    \\qbank_viewquestiontext\\output\\question_text_format::OFF,\n    \\qbank_viewquestiontext\\output\\question_text_format::PLAIN,\n    \\qbank_viewquestiontext\\output\\question_text_format::FULL,\n];\nif (!in_array($format, $validformats)) {\n    $format = \\qbank_viewquestiontext\\output\\question_text_format::OFF;\n}\nquestion_set_or_get_user_preference('qbshowtext', $format, 0, new \\moodle_url('/'));","handlingStrategy":"validation","validationCode":"$validformats = [\\qbank_viewquestiontext\\output\\question_text_format::OFF, \\qbank_viewquestiontext\\output\\question_text_format::PLAIN, \\qbank_viewquestiontext\\output\\question_text_format::FULL];\nif (!in_array($format, $validformats, true)) {\n    $format = \\qbank_viewquestiontext\\output\\question_text_format::OFF;\n}","typeGuard":"function is_valid_question_text_format($value): bool {\n    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);\n}","tryCatchPattern":"try {\n    question_set_or_get_user_preference('qbshowtext', $format, 0, new \\moodle_url('/'));\n} catch (\\invalid_parameter_exception $e) {\n    debugging('Invalid qbshowtext format supplied, defaulting to OFF', DEBUG_DEVELOPER);\n    question_set_or_get_user_preference('qbshowtext', \\qbank_viewquestiontext\\output\\question_text_format::OFF, 0, new \\moodle_url('/'));\n}","preventionTips":["Always source format values from the question_text_format class constants, never literals.","Use in_array(..., true) strict comparison against the allow-list before submitting.","Sanitize/regenerate bookmarked or hand-edited URLs pointing at save.php.","On upgrade, audit stored qbshowtext user preferences for legacy values."],"tags":["moodle","validation","enum","user-preference"],"backgroundTag":"invalid-enum-value","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}