Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7571315

Browse filesBrowse files
committed
minor #7802 [Form] Make difference between data and empty_data options more visisble (HeahDude, wouterj, javiereguiluz)
This PR was merged into the 2.7 branch. Discussion ---------- [Form] Make difference between data and empty_data options more visisble Finishes #6265 Fixes #7299, #7596 Commits ------- 64e3984 Fixed a minor typo df9b878 Fixed a minor typo f375489 Better explain empty_data values d552ea3 Readd mixed type for empty_data, it can be string, array or object 188f0ab Tried to write the important points more explicitly 5f3813d Updated the example add added a caution 4fd2d96 Addressed @stof's comment 242941a improved accuracy 67c93b6 fix typo 87b3542 explicit differences between 'data' & 'empty_data' in FormType
2 parents 77b7fb0 + 64e3984 commit 7571315
Copy full SHA for 7571315

File tree

3 files changed

+44
-24
lines changed
Filter options

3 files changed

+44
-24
lines changed

‎form/use_empty_data.rst

Copy file name to clipboardExpand all lines: form/use_empty_data.rst
+8-1Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@ a ``data_class`` option for your form class, it will default to a new instance
2727
of that class. That instance will be created by calling the constructor
2828
with no arguments.
2929

30-
If you want to override this default behavior, there are two ways to do this.
30+
If you want to override this default behavior, there are two ways to do this:
31+
32+
* `Option 1: Instantiate a new Class`_
33+
* `Option 2: Provide a Closure`_
34+
35+
If you didn't set the ``data_class`` option, you can pass the initial data as
36+
string or pass an array of strings (where the key matches the field name) when
37+
the form type is compound.
3138

3239
Option 1: Instantiate a new Class
3340
---------------------------------
+10-8Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
data
22
~~~~
33

4-
**type**: ``mixed`` **default**: Defaults to field of the underlying object (if there is one)
4+
**type**: ``mixed`` **default**: Defaults to field of the underlying structure.
55

66
When you create a form, each field initially displays the value of the
7-
corresponding property of the form's domain object (if an object is bound
8-
to the form). If you want to override the initial value for the form or
9-
just an individual field, you can set it in the data option::
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-
.. note::
16+
.. caution::
1617
17-
The default values for form fields are taken directly from the underlying
18-
data structure (e.g. an entity or an array). The ``data`` option overrides
19-
this default value.
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 lose it's
21+
persisted value when the form is submitted.

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

Copy file name to clipboardExpand all lines: reference/forms/types/options/empty_data.rst.inc
+26-15Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,35 @@ empty_data
99
1010
DEFAULT_PLACEHOLDER
1111
12-
This option determines what value the field will return when the submitted
13-
value is empty.
14-
15-
But you can customize this to your needs. For example, if you want the
16-
``gender`` choice field to be explicitly set to ``null`` when no value is
17-
selected, you can do it like this::
18-
19-
$builder->add('gender', 'choice', array(
20-
'choices' => array(
21-
'm' => 'Male',
22-
'f' => 'Female'
23-
),
24-
'required' => false,
25-
'placeholder' => 'Choose your gender',
26-
'empty_data' => null
12+
This option determines what value the field will *return* when the submitted
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.
15+
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``
18+
when no value is selected, you can do it like this::
19+
20+
$builder->add('name', null, array(
21+
'required' => false,
22+
'empty_data' => 'John Doe',
2723
));
2824
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+
29+
If a form is compound, you can set ``empty_data`` as an array, object or
30+
closure. See the :doc:`/form/use_empty_data` article for more details about
31+
these options.
32+
2933
.. note::
3034
3135
If you want to set the ``empty_data`` option for your entire form class,
3236
see the :doc:`/form/use_empty_data` article.
37+
38+
.. caution::
39+
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 cast to ``null``. Use a custom data transformer if you explicitly want
43+
to return the empty string.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.