Jump to:

2011 Posts in 1436 Topics by 620 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: 711 Views
  • juergr
    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()


    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
    Forum Moderator
    5508 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
    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>

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

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.