Skip to content
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

new cookbook article on using empty data for form classes #2415

Merged
merged 1 commit into from Apr 4, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions cookbook/form/index.rst
Expand Up @@ -11,3 +11,4 @@ Form
create_custom_field_type
create_form_type_extension
use_virtuals_forms
use_empty_data
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should add the newline at the end IMO to avoid extra diffs (and associated conflicts) when adding another line in the file

47 changes: 47 additions & 0 deletions cookbook/form/use_empty_data.rst
@@ -0,0 +1,47 @@
.. index::
single: Form; Empty data

How to configure Empty Data for a Form Class
============================================

The ``empty_data`` option allows you to specify an empty data set for your
form class. This empty data set would be used if you bind your form, but
haven't yet called ``setData()``.

By default, ``empty_data`` is set to ``null``. Or, if you have specified
a ``data_class`` option for your form class, it will default to a new instance
of that class. That instance will be created by calling the constructor
with no arguments.

If you want to override this default behavior, there are two ways to do this.

Option 1: Instantiate a new Class
---------------------------------

One reason you might use this option is if you want to use a constructor
that takes arguments. Remember, the default ``data_class`` option calls
that constructor with no arguments::

public function getDefaultOptions()
{
return array(
'empty_data' => new User($this->someDependency),
);
}

Option 2: Provide a Closure
---------------------------

Using a closure is the preferred method, since it will only create the object
if it is needed.

The closure must accept a ``FormInterface`` instance as the first argument::

public function getDefaultOptions()
{
return array(
'empty_data' => function (FormInterface $form) {
return new User($form->get('username')->getData());
},
);
}
1 change: 1 addition & 0 deletions cookbook/map.rst.inc
Expand Up @@ -80,6 +80,7 @@
* :doc:`/cookbook/form/create_custom_field_type`
* :doc:`/cookbook/form/create_form_type_extension`
* :doc:`/cookbook/form/use_virtuals_forms`
* :doc:`/cookbook/form/use_empty_data`
* (validation) :doc:`/cookbook/validation/custom_constraint`
* (doctrine) :doc:`/cookbook/doctrine/file_uploads`

Expand Down
5 changes: 5 additions & 0 deletions reference/forms/types/options/empty_data.rst.inc
Expand Up @@ -20,3 +20,8 @@ value is selected, you can do it like this:
'empty_value' => 'Choose your gender',
'empty_data' => null
));

.. note::

If you want to set the ``empty_data`` option for your entire form class,
see the cookbook article :doc:`/cookbook/form/use_empty_data`