Closed
Description
The usage of the empty_data
option for forms should be documented. empty_data
allows to set the data set in the form when it is bound and setData()
wasn't called before.
By default, this is null
or, if data_class
is set, a new instance of that given class (created by using a no-args constructor).
The option can be overridden by setting a static value (1) or by setting a closure (2), that receives a FormInterface
instance as first argument. (2) should be preferred, as it only creates objects on demand.
(1)
public function getDefaultOptions()
{
return array(
'empty_data' => new User($this->someDependency)
);
}
(2)
public function getDefaultOptions()
{
return array(
'empty_data' => function (FormInterface $form) {
return new User($form->get('username')->getData());
}
);
}