Faloude Faloude - 1 year ago 164
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?

Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download