Semicolon
Semicolon

Reputation: 1914

SilverStripe DropdownField default for boolean value

I can't set a default value for a DropdownField

DropdownField::create('Foo', 'Foo', array(true => 'Yes', false => 'No'), $value = true);

When creating a new page, the field shows No false => 'No'. Is it because the $db value for Foo is set to Null by default?

Or is it because the Default parameter for DropdownField should be a string, whereas here it's a boolean value?

Upvotes: 1

Views: 663

Answers (2)

3dgoo
3dgoo

Reputation: 15794

The following will work:

DropdownField::create('Foo', 'Foo', array(true => 'Yes', false => 'No'), 0);

The issue you were having is that you were passing false. I think the code was interpreting that false as no default value being set. Similar as if you passed null. Any value other than null or false should work.

Upvotes: 1

csy_dot_io
csy_dot_io

Reputation: 1199

You could set the defaults with one of those.

private static $defaults = [
  'Foo' => true
];

or if you want to have a more dynamic way

public function populateDefaults() {
  parent::populateDefaults();

  $this->Foo = true;
}

Upvotes: 2

Related Questions