I'm using form object and am trying to set a default option from the options i retrieved from a model. Everywhere I searched it appears it should be possible to set the default value by setDefault. But I can't get it to work. What am I missing?
In my for object the field looks like below. It does populate everything it just sets the default to the first option found. And I want to set the default to 'drie maanden', which means 'three months'.
// Guarantee
$guarantee = new Select('guarantee', GuaranteeType::find(), [
'using' => ['name', 'name'],
'useEmpty' => false,
'emptyText' => 'Select guarantee term',
'emptyValue' => '',
'class' => 'form-control select2',
]);
$guarantee->setDefault();
$guarantee->setLabel('Guarantee');
$guarantee->addValidators(
[
new PresenceOf(
[
"message" => "Selecting a guarantee term is required",
]
)
]
);
$this->add($guarantee);