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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Form Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

DropdownField: can't set value


Go to End


3 Posts   3393 Views

Avatar
juergr

Community Member, 17 Posts

20 December 2012 at 12:22am

Edited: 20/12/2012 12:47am

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()

  Debug::dump($this->value);

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?

Avatar
Willr

Forum Moderator, 5523 Posts

20 December 2012 at 8:13pm

The form value may be overridden later in the process using setValue(), loadDataFrom()

Avatar
juergr

Community Member, 17 Posts

20 December 2012 at 11:23pm

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=""></option>
	<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>
</select>

But it's somehow strange that i have to add the braces by myself...