moodle/moodle · error · moodle_exception
invalidenrolinstance
invalidenrolinstance
Error message
invalidenrolinstance
What it means
Thrown by enrol_guest\form\enrol_form::get_instance() when the requested enrolment instance cannot be resolved: the 'id' (course) or 'instance' request parameter is missing/0, the instance does not exist among the course's ENABLED enrolment instances, or it exists but its enrol method is not 'guest'. enrol_get_instances() is deliberately used so only enabled methods validate. All dynamic-form handlers (validation, submission, context, URL) funnel through get_instance(), so any request with a bad instance id raises this.
Solutions
- Confirm the guest enrolment method is enabled for the course: Course administration > Enrolment methods, set Guest access to enabled.
- Reload the course page to obtain a fresh enrol link with the correct current instance id.
- Verify in code before use: $instances = enrol_get_instances($courseid, true); if (empty($instances[$instanceid]) || $instances[$instanceid]->enrol !== 'guest') { ... }.
- Check the enrol table: SELECT id,status,enrol FROM {enrol} WHERE courseid=? AND enrol='guest' to confirm the instance exists and is enabled (status = ENROL_INSTANCE_ENABLED).
Example fix
// before
$instances = enrol_get_instances($courseid, true);
if (empty($instances[$instanceid]) || $instances[$instanceid]->enrol !== 'guest') {
throw new \moodle_exception('invalidenrolinstance', 'enrol');
}
// after (validate in caller first)
$instances = enrol_get_instances($courseid, ENROL_INSTANCE_ENABLED);
$guest = array_filter($instances, fn($i) => $i->enrol === 'guest');
if (empty($guest)) { redirect(new moodle_url('/course/view.php', ['id'=>$courseid])); } Defensive patterns
Strategy: validation
Validate before calling
$instances = enrol_get_instances($courseid, true);
$valid = !empty($instances[$instanceid]) && $instances[$instanceid]->enrol === 'guest';
if (!$valid) { redirect(new moodle_url('/course/view.php', ['id'=>$courseid])); } Type guard
function isGuestInstance(?stdClass $instance): bool {
return $instance !== null && isset($instance->enrol) && $instance->enrol === 'guest' && isset($instance->id);
} Try / catch
try {
$instance = $form->get_instance();
} catch (\moodle_exception $e) {
if ($e->errorcode === 'invalidenrolinstance') {
redirect(new moodle_url('/course/view.php', ['id'=>$courseid]), get_string('invalidenrolinstance', 'enrol'));
}
throw $e;
} Prevention
- Always pass the current instance id from a freshly loaded course page, not cached links.
- Confirm the guest enrol method is enabled before advertising guest access.
- Check {enrol} rows (enrol='guest', status=enabled) when debugging.
- Regenerate enrol links after deleting/recreating enrolment instances.
When it happens
Trigger: Submitting the guest enrol form with a stale/wrong ?instance= parameter; the guest enrolment method was disabled (so enrol_get_instances($courseid, true) omits it); the instance was deleted; an instance of another enrol method's id is passed; instance=0 when the param is absent.
Common situations: A course page cached a self-enrol link before the guest method was disabled; an admin toggled 'guest access' off while users had the form open; a bookmarked/EMailed enrol URL pointing at an old instance id; form submitted after the instance id changed.
Understand the failure class
Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.
Related errors
- coursehidden
- invalidenrolinstance
- noguestaccess
- A valid forum ID is required to generate a summary report.
- cannotviewcategory
AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14).
Data as JSON: /api/errors/bdb6e4cfe1fdf1e3.
Report an issue: GitHub.
Appendix: source
Thrown at public/enrol/guest/classes/form/enrol_form.php:52
/** @var \stdClass */
protected $instance;
/**
* Returns the instance of the enrolment method
*
* @throws \moodle_exception
* @return \stdClass
*/
protected function get_instance(): \stdClass {
global $DB, $CFG;
require_once($CFG->dirroot . '/lib/enrollib.php');
if ($this->instance === null) {
$courseid = $this->optional_param('id', 0, PARAM_INT);
$instanceid = $this->optional_param('instance', 0, PARAM_INT);
// We need enrol_get_instances() to validate that the enrolment method is enabled.
$instances = enrol_get_instances($courseid, true);
if (empty($instances[$instanceid]) || $instances[$instanceid]->enrol !== 'guest') {
throw new \moodle_exception('invalidenrolinstance', 'enrol');
}
$this->instance = $instances[$instanceid] ?? null;
}
return $this->instance;
}
#[\Override]
public function definition() {
$mform = $this->_form;
$mform->addElement('password', 'guestpassword', get_string('password', 'enrol_guest'));
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'instance');
$mform->setType('instance', PARAM_INT);
}View on GitHub (pinned to 48ea8c3322)