Symfony2CookBook:如何使用虛擬表單域選項

來源:互聯網
上載者:User

  • 原文出處:http://symfony.com/doc/2.1/cookbook/form/use_virtuals_forms.html
  • 原文作者:symfony.com
  • 授權許可:創作共用協議
  • 翻譯人員:FireHare
  • 校對人員:FireHare
  • 適用版本:Symfony 2.1
  • 文章狀態:草譯階段

The virtual form field option can be very useful when you have some duplicated fields in different entities.
當您在不同的實體中擁有一些重複的表單域時,虛擬表單域選項就顯得非常有用。

For example, imagine you have two entities, a Company and a Customer:
舉個例子,想象您有兩個實體,Company和Customer:

Company實體:

 
  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實體:

 
  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.
正如您所見,每個實體都有著一些相同的表單域:address、zipcode、city和country。

Now, you want to build two forms: one for a Company and the second for a Customer.
現在,您要建立兩個表單:一個對應Company,第二個對應Customer。

Start by creating a very simple CompanyType and CustomerType:
首先建立一個非常簡單的CompanyType和CustomerType:

CompanyType類:

 
  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類:

 
  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:
現在我們需要處理那四個重複的表單域。下面是個簡單)的位置表單類型:

 
  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't actually have a location field in each of our entities, so we can't directly link our LocationType to our CompanyType or CustomerType. But we absolutely want to have a dedicated form type to deal with location (remember, DRY!).
實際上在每個實體中我們並沒有location表單域,因此我們不能直接將LocationType鏈入我們的CompanyType或CustomerType中。但我們又絕對想要一個專門處理位置的表單類型記住,DRY!)

The virtual form field option is the solution.
解決這個問題就需要用到虛擬表單域選項了。

We can set the option 'virtual' => true in the setDefaultOptions() method of LocationType and directly start using it in the two original form types.
我們可以在LocationType類中的setDefaultOptions()方法中將選項'virtual'設定為true,並且在兩個原始域類型中直接使用它。

Look at the result:
所下所示:

CompanyType類:

 
  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類:

 
  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 have a foo (or bar) 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!
當virtual選項設為false時預設值),表單組件期望每個相關的對象都有一個foo或bar)屬性,要麼是一些對象,要麼是包含四個位置表單域的數組。當然,在我們的實體中沒有這樣的對象或數組,而且我們也不想那樣做!

With the virtual option set to true, the Form component skips the foo (or bar) property, and instead "gets" and "sets" the 4 location fields directly on the underlying object!
當virtual選項設為true時,表單組件忽略foo或bar)屬性,取而代之的是在相關對象中直接"gets"和"sets"這4個位置表單域!

Instead of setting the virtual option inside LocationType, you can (just like with any options) also pass it in as an array option to the third argument of $builder->add().
如果不使用在LocationType中設定virtual選項,您也可以如其它選項那樣)將其作為一個數組選項發送到$builder->add()的第三個參數。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.