Faloude Faloude - 1 month ago 15
PHP Question

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?

Answer

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;
}