Faloude Faloude - 4 months ago 39
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
is set to Null by default?

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


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

  $this->Foo = true;