Ivan Ivan - 1 year ago 141
PHP Question

Silverstripe selected in DropdownField

How can I set a default selected value in

. I have list of categories and I want to set a default selected item, but I don't know how to do it. Looking on SS API for DropdownField, there does not exist method for

Here is my code:

DropdownField::create('CategoryID', 'Categories')->setSource(Category::get()->map('ID', 'Name'))->setEmptyString('Chose category'),

Answer Source

Use ->setValue with your value (e.g. $iSelectedCategoryID) like this...

DropdownField::create('CategoryID', 'Categories')
    ->setSource(Category::get()->map('ID', 'Name'))
    ->setEmptyString('Chose category')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download