- Source: http://symfony.com/doc/2.1/cookbook/form/use_virtuals_forms.html
- Original Author: symfony.com
- Authorization license: shared creation agreement
- Translated by: FireHare
- Proofreader: FireHare
- Applicable version: Symfony 2.1
- Article status: Grass translation stage
Thevirtual
Form field option can be very useful when you have some duplicated fields in different entities.
When you have repeated form fields in different entities, the virtual form field option is very useful.
For example, imagine you have two entities,Company
AndCustomer
:
For example, assume that you have two entities: Company and Customer:
Company Entity:
- // src/Acme/HelloBundle/Entity/Company.php
- namespace Acme\HelloBundle\Entity;
-
- class Company
- {
- private $name;
- private $website;
-
- private $address;
- private $zipcode;
- private $city;
- private $country;
- }
Customer entity:
- // src/Acme/HelloBundle/Entity/Customer.php
- namespace Acme\HelloBundle\Entity;
-
- class Customer
- {
- private $firstName;
- private $lastName;
-
- private $address;
- private $zipcode;
- private $city;
- private $country;
- }
Like you can see, each entity shares a few of the same fields:address
,zipcode
,city
,country
.
As you can see, each entity has the same form fields: address, zipcode, city, and country.
Now, you want to build two forms: one forCompany
And the second forCustomer
.
Now, you need to create two forms: one corresponding to Company and the other corresponding to Customer.
Start by creating a very simpleCompanyType
AndCustomerType
:
First, create a very simple CompanyType and CustomerType:
CompanyType class:
- // src/Acme/HelloBundle/Form/Type/CompanyType.php
- namespace Acme\HelloBundle\Form\Type;
-
- use Symfony\Component\Form\FormBuilderInterface;
-
- class CompanyType extends AbstractType
- {
- public function buildForm(FormBuilderInterface $builder, array $options)
- {
- $builder
- ->add('name', 'text')
- ->add('website', 'text');
- }
- }
CustomerType class:
- // src/Acme/HelloBundle/Form/Type/CustomerType.php
- namespace Acme\HelloBundle\Form\Type;
-
- use Symfony\Component\Form\FormBuilderInterface;
-
- class CustomerType extends AbstractType
- {
- public function buildForm(FormBuilderInterface $builder, array $options)
- {
- $builder
- ->add('firstName', 'text')
- ->add('lastName', 'text');
- }
- }
Now, we have to deal with the four duplicated fields. Here is a (simple) location form type:
Now we need to process the four repeated form fields. The following is a simple) Location form type:
- // src/Acme/HelloBundle/Form/Type/LocationType.php
- namespace Acme\HelloBundle\Form\Type;
-
- use Symfony\Component\Form\FormBuilderInterface;
- use Symfony\Component\OptionsResolver\OptionsResolverInterface;
-
- class LocationType extends AbstractType
- {
- public function buildForm(FormBuilderInterface $builder, array $options)
- {
- $builder
- ->add('address', 'textarea')
- ->add('zipcode', 'text')
- ->add('city', 'text')
- ->add('country', 'text');
- }
-
- public function setDefaultOptions(OptionsResolverInterface $resolver)
- {
- $resolver->setDefaults(array(
- 'virtual' => true
- ));
- }
-
- public function getName()
- {
- return 'location';
- }
- }
We don'tActuallyHave a location field in each of our entities, so we can't directly link ourLocationType
To ourCompanyType
OrCustomerType
. But we absolutely want to have a dedicated form type to deal with location (remember, DRY !).
In fact, we do not have the location form field in each object, so we cannot directly chain the LocationType into our CompanyType or CustomerType. But we absolutely want a form type that specifically handles the location. Remember, DRY !)
Thevirtual
Form field option is the solution.
To solve this problem, you need to use the virtual form field option.
We can set the option'virtual' => true
InsetDefaultOptions()
MethodLocationType
And directly start using it in the two original form types.
We canIn the LocationType class
In the setDefaultOptions () method
OptionSet 'virtual' to true and use it directly in two original domain types.
Look at the result:
As shown below:
CompanyType class:
- // CompanyType
- public function buildForm(FormBuilderInterface $builder, array $options)
- {
- $builder->add('foo', new LocationType(), array(
- 'data_class' => 'Acme\HelloBundle\Entity\Company'
- ));
- }
CustomerType class:
- // CustomerType
- public function buildForm(FormBuilderInterface $builder, array $options)
- {
- $builder->add('bar', new LocationType(), array(
- 'data_class' => 'Acme\HelloBundle\Entity\Customer'
- ));
- }
With the virtual option set to false (default behavior), the Form Component expects each underlying object to havefoo
(Orbar
) Property that is either some object or array which contains the four location fields. Of course, we don't have this object/array in our entities and we don't want it!
When the virtual option is set to false, the default value is). The form Component expects each Related Object To Have A foo or bar attribute, or an array containing four form fields. Of course, there are no such objects or arrays in our entities, and we don't want to do that either!
With the virtual option set to true, the Form component skipsfoo
(Orbar
) Property, and instead "gets" and "sets" the 4 location fields directly on the underlying object!
When the virtual option is set to true, the form component ignores the foo or bar attribute. Instead, the form fields are directly "gets" and "sets" in the related objects!
Instead of settingvirtual Option insideLocationType , You can (just like with any options) also pass it in as an array option to the third argument$builder->add() . If you do not set the virtual option in LocationType, you can also send it as an array option to the third parameter $ builder-> add. |