Cannot select multiple options when editing form

Cannot select multiple options when editing form

by Daryl Batchelor -
Number of replies: 2

If anyone could help me with this it would be greatly appreciated!  I have created an autocomplete form element like so:

$selected_options = array( '544', '717')
$multiselect = $mform->addElement('course', 'courses_array', get_string('certificate_courseSection','mod_certification'), $courselist, $options);
$multiselect->setMultiple(true);
$multiselect->setValue($selected_options)
I am attempting to auto populate the selected element in the form utilising an array of values comprised of selected course ids like so.

$selected_options = array( '544', '717')

However when I attempt to select multiple default values I cannot do it.

I have tried

$multiselect->setSelected($selected_options)
instead and also
$mform->setDefault('courses_array', $selected_options)
but it does not work.

The only way I can get it to work is if I remove

$multiselect->setMultiple(true)

so it only displays a single choice and use

$multiselect->setValue($selected_options)

- however this only displays the last element in the $selected_options array when I require it to display multiple elements.

Average of ratings: -
In reply to Daryl Batchelor

Re: Cannot select multiple options when editing form

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
This might help explore the possibilities.
https://m.vledevelop.co.uk/local/autocomplete2
In reply to Marcus Green

Re: Cannot select multiple options when editing form

by Daryl Batchelor -
Thanks Marcus,

this helped in finding the issue.

Fix:
The problem was the second parameter I used for the addElement function "courses_array". For some reason using a string parameter with an underscore caused the error??? simply changing it to "coursesarray" solved this... not sure why, but it is working now.

CHANGED THIS: $multiselect = $mform->addElement('course', 'courses_array', get_string('certificate_courseSection','mod_certification'), $courselist, $options);
TO THIS: $multiselect = $mform->addElement('course', 'coursesarray', get_string('certificate_courseSection','mod_certification'), $courselist, $options);