Home  >  Q&A  >  body text

Moodle form cannot be verified after submission

<p>I'm trying to create a form whose structure depends on parameters in the URL. If no parameters are specified in the URL, an error message should be displayed. Based on the id, perform a database query and populate the form data. </p> <p>Example URL: http://127.0.0.1/local/group/signin.php?groupid=14</p> <p>Unfortunately, when I submit the form by clicking the action button, my form fails to validate. It jumps to http://127.0.0.1/local/group/signin.php and displays the error message "Group not found" because there are no parameters in the URL. </p> <p>What am I doing wrong here? </p> <p>signinform.php:</p> <pre class="brush:php;toolbar:false;">class signinform extends moodleform { public function definition() { global $DB; global $USER; $mform = $this->_form; $urlid = $this->_customdata['id']; // Get the passed group ID $message = 'Group not found'; if(is_null($urlid)){ $mform->addElement('html', '<h3>'.\core\notification::error($message).'</h3>'); } else{ // Build forms, execute SQL queries, etc. $this->add_action_buttons(true, 'Submit'); } } function validation($data, $files) { return array(); } }</pre> <p>Login.php:</p> <pre class="brush:php;toolbar:false;">$PAGE->set_url(new moodle_url('/local/schedule/signin.php?')); $PAGE->set_context(\context_system::instance()); $PAGE->set_pagelayout('base'); $PAGE->set_title("Register"); $PAGE->set_heading("Register a group"); global $DB; global $USER; $urlid = $_GET["id"]; $to_form = array('id' => $urlid); // Pass the group ID to the form $mform = new signinform(null, $to_form); $homeurl = new moodle_url('/'); if ($mform->is_cancelled()) { redirect($homeurl, 'Cancelled.'); // Only for testing, never enter here } else if ($fromform = $mform->get_data()) { redirect($homeurl, 'Verification in progress'); // Only for testing, never enter here } echo $OUTPUT->header(); $mform->display(); echo $OUTPUT->footer();</pre></p>
P粉287726308P粉287726308440 days ago520

reply all(1)I'll reply

  • P粉680487967

    P粉6804879672023-08-29 17:01:00

    You need to add a hidden field to the form that contains the 'id' that must be passed to the page, otherwise the id will no longer be present in the parameters of the page when the form is submitted.

    For example (in definition())

    $mform->addElement('hidden', 'id', $urlid);
    $mform->setType('id', PARAM_INT);

    Additionally, in Moodle you should not access $_GET directly - use the wrapper functions required_param() or optional_param() because they:

    • Clean parameters to declared types
    • Automatically get parameters from $_GET or $_POST (important in this case because when you submit the form, the 'id' parameter will become part of the POST data)
    • Handle missing parameters by applying a default value (optional_param) or stopping and displaying an error message (required_param)

    Therefore, your access to $_GET['id'] should be replaced with:

    $urlid = optional_param('id', null, PARAM_INT);

    reply
    0
  • Cancelreply