Skip to content

[Form] Document usage of empty_data #998

Closed
@webmozart

Description

@webmozart

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());
        }
    );
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    FormactionableClear and specific issues ready for anyone to take them.good first issueIdeal for your first contribution! (some Symfony experience may be required)

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions