Skip to content

Commit 434b619

Browse files
committed
[Form] Virtual forms cookbook : added data_class and set virtual to true by default
1 parent 7ae5983 commit 434b619

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

cookbook/form/use_virtuals_forms.rst

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ location form type::
8686
namespace Acme\HelloBundle\Form\Type;
8787

8888
use Symfony\Component\Form\FormBuilderInterface;
89+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
8990

9091
class LocationType extends AbstractType
9192
{
@@ -98,6 +99,13 @@ location form type::
9899
->add('country', 'text');
99100
}
100101

102+
public function setDefaultOptions(OptionsResolverInterface $resolver)
103+
{
104+
$resolver->setDefaults(array(
105+
'virtual' => true
106+
));
107+
}
108+
101109
public function getName()
102110
{
103111
return 'location';
@@ -118,15 +126,19 @@ Look at the result::
118126
// CompanyType
119127
public function buildForm(FormBuilderInterface $builder, array $options)
120128
{
121-
$builder->add('foo', new LocationType());
129+
$builder->add('foo', new LocationType(), array(
130+
'data_class' => 'Acme\HelloBundle\Entity\Company'
131+
));
122132
}
123133

124134
.. code-block:: php
125135
126136
// CustomerType
127137
public function buildForm(FormBuilderInterface $builder, array $options)
128138
{
129-
$builder->add('bar', new LocationType());
139+
$builder->add('bar', new LocationType(), array(
140+
'data_class' => 'Acme\HelloBundle\Entity\Customer'
141+
));
130142
}
131143
132144
With the virtual option set to false (default behavior), the Form Component

0 commit comments

Comments
 (0)