Reputation: 1914
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
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
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