@@ -10,35 +10,31 @@ empty_data
1010DEFAULT_PLACEHOLDER
1111
1212This option determines what value the field will return when the submitted
13- value is empty. It does not set an initial value if none is provided when
14- the form is rendered in a view (see ``data`` or ``placeholder`` options).
13+ value is empty (or missing). It does not set an initial value if none is
14+ provided when the form is rendered in a view (see ``data`` or ``placeholder``
15+ options).
1516
1617It helps you handling form submission and you can customize this to your needs.
17- For example, if you want the ``gender `` choice field to be explicitly set to ``Male ``
18+ For example, if you want the ``name `` field to be explicitly set to ``John Doe ``
1819when no value is selected, you can do it like this::
1920
20- $builder->add('gender', 'choice', array(
21- 'choices' => array(
22- 'm' => 'Male',
23- 'f' => 'Female',
24- ),
21+ $builder->add('name', null, array(
2522 'required' => false,
26- 'empty_value' => 'Choose your gender',
27- 'empty_data' => 'm',
23+ 'empty_data' => 'John Doe',
2824 ));
2925
30- If a form is compound, you can set ``empty_data`` as an array with fields names
26+ If a form is compound, you can set ``empty_data`` as an array with field names
3127as keys and submitted values as string values (or arrays if nested fields are
3228also compound).
3329
34- .. caution::
35-
36- In this example, the choice field is not set as ``multiple``. If it was
37- ``empty_data`` option should be an array of submitted string values::
38-
39- 'empty_data' => array('m'),
40-
4130.. note::
4231
4332 If you want to set the ``empty_data`` option for your entire form class,
4433 see the cookbook article :doc:`/cookbook/form/use_empty_data`.
34+
35+ .. caution::
36+
37+ When using `empty_data` as an empty string, the form will always return
38+ ``null``. If you need to get an empty string to be returned, you should
39+ use a data transformer, see the cookbook article
40+ :doc:`cookbook/form/data_transformers`.
0 commit comments