@@ -10,35 +10,30 @@ empty_data
10
10
DEFAULT_PLACEHOLDER
11
11
12
12
This 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).
15
16
16
17
It 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 ``
18
19
when no value is selected, you can do it like this::
19
20
20
- $builder->add('gender', 'choice', array(
21
- 'choices' => array(
22
- 'm' => 'Male',
23
- 'f' => 'Female',
24
- ),
21
+ $builder->add('name', null, array(
25
22
'required' => false,
26
- 'placeholder' => 'Choose your gender',
27
- 'empty_data' => 'm',
23
+ 'empty_data' => 'John Doe',
28
24
));
29
25
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
31
27
as keys and submitted values as string values (or arrays if nested fields are
32
28
also compound).
33
29
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
-
41
30
.. note::
42
31
43
32
If you want to set the ``empty_data`` option for your entire form class,
44
33
see the :doc:`/form/use_empty_data` article.
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 :doc:`/form/data_transformers` article.
0 commit comments