Skip to content

Commit dbfcaff

Browse files
committed
Adapted the docs to the rename of the "virtual" form option to "inherit_data"
1 parent 235513a commit dbfcaff

File tree

3 files changed

+32
-40
lines changed

3 files changed

+32
-40
lines changed

cookbook/form/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ Form
1010
form_collections
1111
create_custom_field_type
1212
create_form_type_extension
13-
use_virtuals_forms
13+
inherit_data_option

cookbook/form/use_virtuals_forms.rst renamed to cookbook/form/inherit_data_option.rst

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
.. index::
2-
single: Form; Virtual forms
2+
single: Form; The "inherit_data" option
33

4-
How to use the Virtual Form Field Option
5-
========================================
4+
Reducing Code Duplication with "inherit_data"
5+
=============================================
66

7-
The ``virtual`` form field option can be very useful when you have some
8-
duplicated fields in different entities.
9-
10-
For example, imagine you have two entities, a ``Company`` and a ``Customer``::
7+
The ``inherit_data`` form field option can be very useful when you have some
8+
duplicated fields in different entities. For example, imagine you have two
9+
entities, a ``Company`` and a ``Customer``::
1110

1211
// src/Acme/HelloBundle/Entity/Company.php
1312
namespace Acme\HelloBundle\Entity;
@@ -39,13 +38,10 @@ For example, imagine you have two entities, a ``Company`` and a ``Customer``::
3938
private $country;
4039
}
4140
42-
Like you can see, each entity shares a few of the same fields: ``address``,
41+
As you can see, each entity shares a few of the same fields: ``address``,
4342
``zipcode``, ``city``, ``country``.
4443

45-
Now, you want to build two forms: one for a ``Company`` and the second for
46-
a ``Customer``.
47-
48-
Start by creating a very simple ``CompanyType`` and ``CustomerType``::
44+
Let's build two forms for these entities, ``CompanyType`` and ``CustomerType``::
4945

5046
// src/Acme/HelloBundle/Form/Type/CompanyType.php
5147
namespace Acme\HelloBundle\Form\Type;
@@ -79,8 +75,9 @@ Start by creating a very simple ``CompanyType`` and ``CustomerType``::
7975
}
8076
}
8177
82-
Now, to deal with the four duplicated fields. Here is a (simple)
83-
location form type::
78+
Instead of including the duplicated fields ``address``, ``zipcode``, ``city``
79+
and ``country``in both of these forms, we will create a third form for that.
80+
We will call this form simply ``LocationType``::
8481

8582
// src/Acme/HelloBundle/Form/Type/LocationType.php
8683
namespace Acme\HelloBundle\Form\Type;
@@ -102,7 +99,7 @@ location form type::
10299
public function setDefaultOptions(OptionsResolverInterface $resolver)
103100
{
104101
$resolver->setDefaults(array(
105-
'virtual' => true
102+
'inherit_data' => true
106103
));
107104
}
108105

@@ -112,46 +109,41 @@ location form type::
112109
}
113110
}
114111

115-
You don't *actually* have a location field in each of your entities, so you
116-
can't directly link ``LocationType`` to ``CompanyType`` or ``CustomerType``.
117-
But you absolutely want to have a dedicated form type to deal with location (remember, DRY!).
112+
The location form has an interesting option set, namely ``inherit_data``. This
113+
option lets the form inherit its data from its parent form. If embedded in
114+
the company form, the fields of the location form will access the properties of
115+
the ``Company`` instance. If embedded in the customer form, the fields will
116+
access the properties of the ``Customer`` instance instead. Easy, eh?
118117

119-
The ``virtual`` form field option is the solution.
118+
.. note::
120119

121-
You can set the option ``'virtual' => true`` in the ``setDefaultOptions()`` method
122-
of ``LocationType`` and directly start using it in the two original form types.
120+
Instead of setting the ``inherit_data`` option inside ``LocationType``, you
121+
can also (just like with any option) pass it in the third argument of
122+
``$builder->add()``.
123123

124-
Look at the result::
124+
Let's make this work by adding the location form to our two original forms::
125125

126-
// CompanyType
126+
// src/Acme/HelloBundle/Form/Type/CompanyType.php
127127
public function buildForm(FormBuilderInterface $builder, array $options)
128128
{
129+
// ...
130+
129131
$builder->add('foo', new LocationType(), array(
130132
'data_class' => 'Acme\HelloBundle\Entity\Company'
131133
));
132134
}
133135

134136
.. code-block:: php
135137
136-
// CustomerType
138+
// src/Acme/HelloBundle/Form/Type/CustomerType.php
137139
public function buildForm(FormBuilderInterface $builder, array $options)
138140
{
141+
// ...
142+
139143
$builder->add('bar', new LocationType(), array(
140144
'data_class' => 'Acme\HelloBundle\Entity\Customer'
141145
));
142146
}
143147
144-
With the virtual option set to false (default behavior), the Form Component
145-
expects each underlying object to have a ``foo`` (or ``bar``) property that
146-
is either some object or array which contains the four location fields.
147-
Of course, you don't have this object/array in your entities and you don't want it!
148-
149-
With the virtual option set to true, the Form component skips the ``foo`` (or ``bar``)
150-
property, and instead "gets" and "sets" the 4 location fields directly
151-
on the underlying object!
152-
153-
.. note::
154-
155-
Instead of setting the ``virtual`` option inside ``LocationType``, you
156-
can (just like with any options) also pass it in as an array option to
157-
the third argument of ``$builder->add()``.
148+
That's it! You have extracted duplicated field definitions to a separate
149+
location form that you can reuse wherever you need it.

cookbook/map.rst.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
* :doc:`/cookbook/form/form_collections`
8080
* :doc:`/cookbook/form/create_custom_field_type`
8181
* :doc:`/cookbook/form/create_form_type_extension`
82-
* :doc:`/cookbook/form/use_virtuals_forms`
82+
* :doc:`/cookbook/form/inherit_data_option`
8383
* (validation) :doc:`/cookbook/validation/custom_constraint`
8484
* (doctrine) :doc:`/cookbook/doctrine/file_uploads`
8585

0 commit comments

Comments
 (0)