File tree Expand file tree Collapse file tree 2 files changed +20
-6
lines changed
reference/forms/types/options Expand file tree Collapse file tree 2 files changed +20
-6
lines changed Original file line number Diff line number Diff line change 66When you attach a form type to a form, it becomes a field that initially maps
77the value of the corresponding property or key of the form's domain data. If
88you 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::
1010
1111 $builder->add('token', 'hidden', array(
1212 'data' => 'abcdef',
1313 ));
1414
15+ // Is the same as
16+ $resolver->setDefault('data' => array('token' => 'abcdef'));
17+
1518.. note::
1619
1720 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
1922 key of an array. The ``data`` option overrides this default value.
Original file line number Diff line number Diff line change 11empty_data
22~~~~~~~~~~
33
4- **type**: ``mixed``
4+ **type**: ``string`` or ``array`` when the form is compound
55
66.. This file should only be included with start-after or end-before that's
77 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
1414the form is rendered in a view (see ``data`` or ``placeholder`` options).
1515
1616It 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 ``
1818when no value is selected, you can do it like this::
1919
2020 $builder->add('gender', 'choice', array(
2121 'choices' => array(
2222 'm' => 'Male',
23- 'f' => 'Female'
23+ 'f' => 'Female',
2424 ),
2525 'required' => false,
2626 'empty_value' => 'Choose your gender',
27- 'empty_data' => null
27+ 'empty_data' => 'm',
2828 ));
2929
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+
3041.. note::
3142
3243 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