Jump to:

1970 Posts in 1275 Topics by 607 members

Form Questions

SilverStripe Forums » Form Questions » DropdownField: can't set value

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

Page: 1
Go to End
Author Topic: 545 Views
  • juergr
    Avatar
    Community Member
    17 Posts

    DropdownField: can't set value Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: DropdownField: can't set value Link to this post

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

  • juergr
    Avatar
    Community Member
    17 Posts

    Re: DropdownField: can't set value Link to this post

    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...

    545 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.