File tree 2 files changed +20
-6
lines changed
Filter options
reference/forms/types/options
2 files changed +20
-6
lines changed
Original file line number Diff line number Diff line change 6
6
When you attach a form type to a form, it becomes a field that initially maps
7
7
the value of the corresponding property or key of the form's domain data. If
8
8
you want to override the initial value which will be rendered in the view for
9
- the form or any field, you can set it in the data option::
9
+ the form or any nested field, you can set it in the data option::
10
10
11
11
$builder->add('token', 'hidden', array(
12
12
'data' => 'abcdef',
13
13
));
14
14
15
+ // Is the same as
16
+ $resolver->setDefault('data' => array('token' => 'abcdef'));
17
+
15
18
.. note::
16
19
17
20
The default values for form fields are taken directly from the underlying
18
- data structure matching the field name with a property of an object or a
21
+ data structure matching the field's name with a property of an object or a
19
22
key of an array. The ``data`` option overrides this default value.
Original file line number Diff line number Diff line change 1
1
empty_data
2
2
~~~~~~~~~~
3
3
4
- **type**: ``mixed``
4
+ **type**: ``string`` or ``array`` when the form is compound
5
5
6
6
.. This file should only be included with start-after or end-before that's
7
7
set to this placeholder value. Its purpose is to let us include only
@@ -14,19 +14,30 @@ value is empty. It does not set an initial value if none is provided when
14
14
the form is rendered in a view (see ``data`` or ``placeholder`` options).
15
15
16
16
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 ``null ``
17
+ For example, if you want the ``gender`` choice field to be explicitly set to ``Male ``
18
18
when no value is selected, you can do it like this::
19
19
20
20
$builder->add('gender', 'choice', array(
21
21
'choices' => array(
22
22
'm' => 'Male',
23
- 'f' => 'Female'
23
+ 'f' => 'Female',
24
24
),
25
25
'required' => false,
26
26
'placeholder' => 'Choose your gender',
27
- 'empty_data' => null
27
+ 'empty_data' => 'm',
28
28
));
29
29
30
+ If a form is compound, you can set ``empty_data`` as an array with fields names
31
+ as keys and submitted values as string values (or arrays if nested fields are
32
+ also compound).
33
+
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
+
30
41
.. note::
31
42
32
43
If you want to set the ``empty_data`` option for your entire form class,
You can’t perform that action at this time.
0 commit comments