Skip to content

[Form] Rename "empty_data" to "data_factory" #5939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
webmozart opened this issue Nov 8, 2012 · 13 comments
Closed

[Form] Rename "empty_data" to "data_factory" #5939

webmozart opened this issue Nov 8, 2012 · 13 comments

Comments

@webmozart
Copy link
Contributor

The current option "empty_data" is easily misunderstood (see #4715 and #5906). While the option would intuitively set the data that is returned from the field if the field is empty, it in fact sets the view data used if the field is empty, which is then transformed back to model format. So, like mentioned in #5906, setting "empty_data" to an empty string will not lead to the field returning an empty string, because empty strings are transformed to null by default.

The major use case for "empty_data" is thus to create objects that fields can be mapped into.

Example 1:

$options = array(
    'empty_data' => new User();
);
$form = $factory->createBuilder('form', $options)
    ->add('username', 'text')
    ->add('password', 'password')
    ->getForm();

Example 2:

$options = array(
    'empty_data' => function (FormInterface $form, $data) {
        return new User($data['username']);
    }
);
$form = $factory->createBuilder('form', $options)
    ->add('username', 'text', array('mapped' => false))
    ->add('password', 'password')
    ->getForm();

Consequently, I propose to deprecate "empty_data" and rename it to something more intuitive, like "data_constructor":

$options = array(
    'data_constructor' => function (FormInterface $form, $data) {
        return new User($data['username']);
    }
);
$form = $factory->createBuilder('form', $options)
    ->add('username', 'text', array('mapped' => false))
    ->add('password', 'password')
    ->getForm();

We can probably also deprecate the option to pass objects directly to the option instead of closures (see Example 1). IMO objects should be created on demand, not precautiously.

Once the deprecation phase is over (2.3) we can reintroduce "empty_data" with a new meaning and fix #4715 and #5906.

@webmozart
Copy link
Contributor Author

An alternative name for this option is "create_data" or "new_data":

$options = array(
    'new_data' => function (FormInterface $form, $data) {
        return new User($data['username']);
    }
);
$form = $factory->createBuilder('form', $options)
    ->add('username', 'text', array('mapped' => false))
    ->add('password', 'password')
    ->getForm();

@dlsniper
Copy link
Contributor

dlsniper commented Jan 8, 2013

@bschussek would default_data make more sense since it's returning some something by default if the field is empty when the field is empty?

@webmozart
Copy link
Contributor Author

@dlsniper default_data is unfortunately as misleading as empty_data. The default data is stored in the data option. The result of this option is only really relevant for data mapping, i.e. for creating objects that data can be mapped into.

Should the data mapping ever be outsourced into a serializer as proposed in #5480, this option would move to that serializer as well.

@lyrixx
Copy link
Member

lyrixx commented Jan 24, 2013

👍 with this rename.

What about create_data ?

@xphere
Copy link
Contributor

xphere commented Mar 10, 2013

I was also setting empty_data to the wrong value because the name is misleading. I think data_constructor is way more self-understandable, so I'm 👍 with this. Can #5906 be closed then?

@webmozart
Copy link
Contributor Author

I like create_data.

@docteurklein
Copy link
Contributor

create_data is indeed more clear.

@webmozart
Copy link
Contributor Author

Other idea that came up in the comments to William's blog post: data_factory. This is my favorite so far.

@nicolopignatelli
Copy link

+1 for data_factory, it's the most explanatory option IMO

@giosh94mhz
Copy link
Contributor

I'm also +1 for data_factory. empty_data is a bit puzzling...

Any chance to see this change merged in 2.7? :)

@webmozart webmozart changed the title [Form] Rename "empty_data" to "create_data" [Form] Rename "empty_data" to "data_factory" Mar 2, 2016
@vudaltsov
Copy link
Contributor

I think that since #18357 empty_data as an option name makes sense again, so this issue can be closed.

ping @HeahDude

@HeahDude
Copy link
Contributor

I agree that changing the name has a cost that does not worth it IMHO.

This option is confusing by design, and I hope the latest change in the docs will help with that, even if we can still enhance some stuff there.

👍 for closing here though.

@xabbuh xabbuh closed this as completed Dec 12, 2018
@xabbuh
Copy link
Member

xabbuh commented Dec 12, 2018

I agree with @vudaltsov and @HeahDude

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests