A required parameter (id) was missing

1.6k views Asked by At

I am trying to move the selected items from one table to another and get this error:

A required parameter (id) was missing

$objs = array();
$objs[0] =& $mform->createElement('select', 'choosedepartments', get_string('Choose', 'dlist'), $table, $choices, 'size="25"');
$objs[0]->setMultiple(true);

$objs[1] =& $mform->createElement('select', "selecteddepartments", get_string('Select', 'dlist'), $schoices, 'size="15"');
$objs[1]->setMultiple(true);

$grp =& $mform->addElement('group', 'departmentgrp', get_string('Department list', 'dlist'), $objs, ' ', false);

//$objs[] =& $mform->addElement('submit', 'select', "Select");

$objs = array();
$objs[] =& $mform->createElement('submit', 'addsel', get_string('addsel', 'dlist'));
$objs[] =& $mform->createElement('submit', 'removesel', get_string('removesel', 'dlist'));
$objs[] =& $mform->createElement('submit', 'addall', get_string('addall', 'dlist'));
$objs[] =& $mform->createElement('submit', 'removeall', get_string('removeall', 'dlist'));
$grp =& $mform->addElement('group', 'buttonsgrp', get_string('selectedlist', 'dlist'), $objs, array(' ', '<br />'), false);
1

There are 1 answers

0
davosmith On BEST ANSWER

Why all the '=&' lines? Unless you're using PHP 4 (which isn't compatible with recent versions of Moodle), all objects are passed by reference without it.

It's not entirely clear what code is calling this form, but I assume that the page displaying the form has a line 'required_param('id', PARAM_INT);' somewhere in it. To fix this, you need to add an element in your form called 'id':

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

Whatever code is initialising the form, will also need to use 'setData' to pass in the correct value for 'id'.

e.g.

$id = required_param('id', PARAM_INT); // Something like this line should already exist in the code.
...
$form = new my_form(); // Something like this line should already exist in the code.
...
$form->setData(array('id' => $id));

After that, should go the usual 'if ($form->isCancelled())', 'if ($data = $form->getData())' and '$form->display()' code.