Skip to content

Commit 188f0ab

Browse files
committed
Tried to write the important points more explicitly
1 parent 5f3813d commit 188f0ab

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,19 @@ data
33

44
**type**: ``mixed`` **default**: Defaults to field of the underlying structure.
55

6-
When you attach a form type to a form, it becomes a field that initially maps
7-
the value of the corresponding property or key of the form's domain data. If
8-
you want to override the initial value which will be rendered in the view for
9-
the form or any nested field, you can set it in the data option::
6+
When you create a form, each field initially displays the value of the
7+
corresponding property of the form's domain data (e.g. if you bind an object to
8+
the form). If you want to override this initial value for the form or
9+
an individual field, you can set it in the data option::
10+
1011
1112
$builder->add('token', 'hidden', array(
1213
'data' => 'abcdef',
1314
));
1415
15-
// Is the same as
16-
$resolver->setDefault('data' => array('token' => 'abcdef'));
17-
18-
.. note::
16+
.. caution::
1917
20-
The default values for form fields are taken directly from the underlying
21-
data structure matching the field's name with a property of an object or a
22-
key of an array. The ``data`` option overrides this default value.
23-
It means that when the data passed to the form is an object you want to
24-
edit, the `data` option will also override the value already set.
18+
The ``data`` option *always* overrides the value taken from the domain data
19+
(object) when rendering. This means the object value is also overriden when
20+
the form edits an already persisted object, causing it to loose it's
21+
persisted value when the form is submitted.

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,23 @@ empty_data
99

1010
DEFAULT_PLACEHOLDER
1111

12-
This option determines what value the field will return when the submitted
12+
This option determines what value the field will *return* when the submitted
1313
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).
14+
provided when the form is rendered in a view.
1615

17-
It helps you handling form submission and you can customize this to your needs.
18-
For example, if you want the ``name`` field to be explicitly set to ``John Doe``
16+
This means it helps you handling form submission with blank fields. For
17+
example, if you want the ``name`` field to be explicitly set to ``John Doe``
1918
when no value is selected, you can do it like this::
2019

2120
$builder->add('name', null, array(
22-
'required' => false,
23-
'empty_data' => 'John Doe',
21+
'required' => false,
22+
'empty_data' => 'John Doe',
2423
));
2524

25+
This will still render an empty text box, but upon submission the ``John Doe``
26+
value will be set. Use the ``data`` or ``placeholder`` options to show this
27+
initial value in the rendered form.
28+
2629
If a form is compound, you can set ``empty_data`` as an array with field names
2730
as keys and submitted values as string values (or arrays if nested fields are
2831
also compound).
@@ -34,6 +37,7 @@ also compound).
3437

3538
.. caution::
3639

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.
40+
:doc:`Form data transformers </form/data_transformers>` will still be
41+
applied to the ``empty_data`` value. This means that an empty string will
42+
be casted to ``null``. Use a custom data transformer if you explicitly want
43+
to return the empty string.

0 commit comments

Comments
 (0)