Skip to main content
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
If I try to set the current selected value of a dropdown field, nothing happens
I tried it different ways:
$members = Member::get()->map()->toArray();
DropdownField('Test', 'Test', $members, Member::currentUserID());
DropdownField('Test', 'Test', $members)->setValue(Member::currentUserID());
DropdownField('Test', 'Test', $members)->setValue(1);
None of these lines above seem to work.
If I call in DropdownField::construct()
The correct value is returned. But if I insert the same line of code in DropdownField::Field() I get an empty value back.
Am I missing something or is this just not the way it should work?
The form value may be overridden later in the process using setValue(), loadDataFrom()
OK, found the problem:
If I create the Field like this (the name of the field must have "" at the end):
DropdownField::create('Test', 'Testfield', Member::get()->map('ID', 'Title')->toArray(), 3)->setHasEmptyDefault(true)
I get (as expected) this:
<select name="Test" class="chosendropdown chosen" id="BootstrapForm_EditForm_Test">
<option value="2">User 2</option>
<option value="4">User 4</option>
<option value="3" selected="selected">User 3</option>
<option value="1">User 1</option>
But it's somehow strange that i have to add the braces by myself...