Moodle Forms - Select Menu - Selected Option

Moodle Forms - Select Menu - Selected Option

by Conn Warwicker -
Number of replies: 2
Picture of Core developers Picture of Plugin developers

Hi,

Could anyone advise me how when using the moodle forms I set what the selected item of a select menu is by default, e.g. when you edit an item instead of create a new item, the form will need to be filled in with values.

I have this as the creation of the select element:

$mform->addElement('select', 'criteria['.$crit['qualID'].']['.$unit->get_id().'][' . $crit['id'] . ']', $criterion->get_name(), $possibleValues);

Is it something I need to add in to that as another parameter? Or is it a rule I can aply to the element?

Or..?


Cheers.


Average of ratings: -
In reply to Conn Warwicker

Re: Moodle Forms - Select Menu - Selected Option

by Conn Warwicker -
Picture of Core developers Picture of Plugin developers

Never mind I found it.

Needed to use the setSelected() method on the element



In reply to Conn Warwicker

Re: Moodle Forms - Select Menu - Selected Option

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

You shouldn't need to use the 'setSelected()' function - if the existing data is handed over to the form using the 'set_data()' function, then the select should be set correctly (although, in this case, I suspect the complex name of the select may be related to the data not matching up properly).