Symfony2CookBook: how to use virtual form field options

Source: Internet
Author: User

  • 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

ThevirtualForm 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,CompanyAndCustomer:
For example, assume that you have two entities: Company and Customer:

Company Entity:

 
 
  1. // src/Acme/HelloBundle/Entity/Company.php 
  2. namespace Acme\HelloBundle\Entity; 
  3.  
  4. class Company 
  5.     private $name; 
  6.     private $website; 
  7.  
  8.     private $address; 
  9.     private $zipcode; 
  10.     private $city; 
  11.     private $country; 

Customer entity:

 
 
  1. // src/Acme/HelloBundle/Entity/Customer.php 
  2. namespace Acme\HelloBundle\Entity; 
  3.  
  4. class Customer 
  5.     private $firstName; 
  6.     private $lastName; 
  7.  
  8.     private $address; 
  9.     private $zipcode; 
  10.     private $city; 
  11.     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 forCompanyAnd 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 simpleCompanyTypeAndCustomerType:
First, create a very simple CompanyType and CustomerType:

CompanyType class:

 
 
  1. // src/Acme/HelloBundle/Form/Type/CompanyType.php 
  2. namespace Acme\HelloBundle\Form\Type; 
  3.  
  4. use Symfony\Component\Form\FormBuilderInterface; 
  5.  
  6. class CompanyType extends AbstractType 
  7.     public function buildForm(FormBuilderInterface $builder, array $options) 
  8.     { 
  9.         $builder 
  10.             ->add('name', 'text') 
  11.             ->add('website', 'text'); 
  12.     } 

CustomerType class:

 
 
  1. // src/Acme/HelloBundle/Form/Type/CustomerType.php 
  2. namespace Acme\HelloBundle\Form\Type; 
  3.  
  4. use Symfony\Component\Form\FormBuilderInterface; 
  5.  
  6. class CustomerType extends AbstractType 
  7.     public function buildForm(FormBuilderInterface $builder, array $options) 
  8.     { 
  9.         $builder 
  10.             ->add('firstName', 'text') 
  11.             ->add('lastName', 'text'); 
  12.     } 

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:

 
 
  1. // src/Acme/HelloBundle/Form/Type/LocationType.php 
  2. namespace Acme\HelloBundle\Form\Type; 
  3.  
  4. use Symfony\Component\Form\FormBuilderInterface; 
  5. use Symfony\Component\OptionsResolver\OptionsResolverInterface; 
  6.  
  7. class LocationType extends AbstractType 
  8.     public function buildForm(FormBuilderInterface $builder, array $options) 
  9.     { 
  10.         $builder 
  11.             ->add('address', 'textarea') 
  12.             ->add('zipcode', 'text') 
  13.             ->add('city', 'text') 
  14.             ->add('country', 'text'); 
  15.     } 
  16.  
  17.     public function setDefaultOptions(OptionsResolverInterface $resolver) 
  18.     { 
  19.         $resolver->setDefaults(array( 
  20.             'virtual' => true 
  21.         )); 
  22.     } 
  23.  
  24.     public function getName() 
  25.     { 
  26.         return 'location'; 
  27.     } 

We don'tActuallyHave a location field in each of our entities, so we can't directly link ourLocationTypeTo ourCompanyTypeOrCustomerType. 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 !)

ThevirtualForm field option is the solution.
To solve this problem, you need to use the virtual form field option.

We can set the option'virtual' => trueInsetDefaultOptions()MethodLocationTypeAnd directly start using it in the two original form types.
We canIn the LocationType classIn the setDefaultOptions () methodOptionSet 'virtual' to true and use it directly in two original domain types.

Look at the result:
As shown below:

CompanyType class:

 
 
  1. // CompanyType 
  2. public function buildForm(FormBuilderInterface $builder, array $options) 
  3.     $builder->add('foo', new LocationType(), array( 
  4.         'data_class' => 'Acme\HelloBundle\Entity\Company' 
  5.     )); 

CustomerType class:

 
 
  1. // CustomerType 
  2. public function buildForm(FormBuilderInterface $builder, array $options) 
  3.     $builder->add('bar', new LocationType(), array( 
  4.         'data_class' => 'Acme\HelloBundle\Entity\Customer' 
  5.     )); 

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 settingvirtualOption 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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.