Skip to content

Commit e93ccf1

Browse files
committed
Merge pull request symfony#2415 from joelclermont/issue_998
new cookbook article on using empty data for form classes
2 parents 824e097 + 9063d67 commit e93ccf1

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

cookbook/form/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ Form
1111
create_custom_field_type
1212
create_form_type_extension
1313
use_virtuals_forms
14+
use_empty_data

cookbook/form/use_empty_data.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
.. index::
2+
single: Form; Empty data
3+
4+
How to configure Empty Data for a Form Class
5+
============================================
6+
7+
The ``empty_data`` option allows you to specify an empty data set for your
8+
form class. This empty data set would be used if you bind your form, but
9+
haven't yet called ``setData()``.
10+
11+
By default, ``empty_data`` is set to ``null``. Or, if you have specified
12+
a ``data_class`` option for your form class, it will default to a new instance
13+
of that class. That instance will be created by calling the constructor
14+
with no arguments.
15+
16+
If you want to override this default behavior, there are two ways to do this.
17+
18+
Option 1: Instantiate a new Class
19+
---------------------------------
20+
21+
One reason you might use this option is if you want to use a constructor
22+
that takes arguments. Remember, the default ``data_class`` option calls
23+
that constructor with no arguments::
24+
25+
public function getDefaultOptions()
26+
{
27+
return array(
28+
'empty_data' => new User($this->someDependency),
29+
);
30+
}
31+
32+
Option 2: Provide a Closure
33+
---------------------------
34+
35+
Using a closure is the preferred method, since it will only create the object
36+
if it is needed.
37+
38+
The closure must accept a ``FormInterface`` instance as the first argument::
39+
40+
public function getDefaultOptions()
41+
{
42+
return array(
43+
'empty_data' => function (FormInterface $form) {
44+
return new User($form->get('username')->getData());
45+
},
46+
);
47+
}

cookbook/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
* :doc:`/cookbook/form/create_custom_field_type`
8181
* :doc:`/cookbook/form/create_form_type_extension`
8282
* :doc:`/cookbook/form/use_virtuals_forms`
83+
* :doc:`/cookbook/form/use_empty_data`
8384
* (validation) :doc:`/cookbook/validation/custom_constraint`
8485
* (doctrine) :doc:`/cookbook/doctrine/file_uploads`
8586

reference/forms/types/options/empty_data.rst.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,8 @@ value is selected, you can do it like this:
2020
'empty_value' => 'Choose your gender',
2121
'empty_data' => null
2222
));
23+
24+
.. note::
25+
26+
If you want to set the ``empty_data`` option for your entire form class,
27+
see the cookbook article :doc:`/cookbook/form/use_empty_data`

0 commit comments

Comments
 (0)