Moodle form cannot be verified after submission
P粉287726308
P粉287726308 2023-08-28 21:01:24
0
1
454

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.

Example URL: http://127.0.0.1/local/group/signin.php?groupid=14

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.

What am I doing wrong here?

signinform.php:

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', '

'.\core\notification::error($message).'

'); } else{ // Build forms, execute SQL queries, etc. $this->add_action_buttons(true, 'Submit'); } } function validation($data, $files) { return array(); } }

Login.php:

$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();

P粉287726308
P粉287726308

reply all (1)
P粉680487967

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);
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!