2016-06-22 52 views
0

我想學習如何在symfony的3 建立形式下面一些教程,最佳實踐我已經建立了一個PersonTypeSymfony的3 - 爲concating形式塊

class PersonType extends AbstractType { 

    public function buildForm(FormBuilderInterface $builder, array $options) { 

     $builder->add('gender', ChoiceType::class, array('label' => 'Anrede', 'choices' => array('Herr' => 'Herr', 'Frau' => 'Frau'), 'attr' => array('class' => 'form-control'))) 
       ->add('title', TextType::class, array('label' => 'Titel', 'attr' => array('class' => 'form-control'))) 
       ->add('firstname', TextType::class, array('label' => 'Vorname', 'attr' => array('class' => 'form-control'))) 
       ->add('lastname', TextType::class, array('label' => 'Nachname', 'attr' => array('class' => 'form-control'))) 
       ->add('birthdate', DateType::class, array('label' => 'Geburtsdatum', 'attr' => array('class' => 'form-control'))) 

       ->add('street', TextType::class, array('label' => 'Straße', 'attr' => array('class' => 'form-control'))) 
       ->add('streetnumber', TextType::class, array('label' => 'Hausnummer', 'attr' => array('class' => 'form-control'))) 
       ->add('zip', TextType::class, array('label' => 'PLZ', 'attr' => array('class' => 'form-control'))) 
       ->add('city', TextType::class, array('label' => 'Stadt', 'attr' => array('class' => 'form-control'))) 

       ->add('email', TextType::class, array('label' => 'E-Mail', 'attr' => array('class' => 'form-control')));   
     } 

    public function getName() { 
     return 'person'; 
     }  

    } 

和一些其他類型。

在控制器我有

$person = new Person(); 
$form = $this->createForm(PersonType::class, $person); 

我現在的問題是,如何我現在CONCAT的PersonType一些其他類型得到一個表出來的嗎?然後,我如何設置提交按鈕?

回答

0

你不能連接,但你可以包含幾個形式的字段子集。

在這裏,你有Symfony的文檔中的一個很好的例子:

http://symfony.com/doc/current/cookbook/form/inherit_data_option.html

回顧:

  • 創建您的子域的表單,用選項 'inherit_data'=>真。
  • 以另一種形式作爲字段使用它。
+0

嗨,謝謝你的回答。我tryingand搜索網頁,但是當我做了'$ resolver-> setDefaults(陣列( \t \t \t \t 'inherit_data'=>真 \t \t \t \t));'所描述的,我得到一個錯誤* *編譯錯誤:AppBundle \ Form \ LocationType :: configureOptions()聲明必須與Symfony \ Component \ Form \ FormTypeInterface :: configureOptions兼容(Symfony \ Component \ OptionsResolver \ OptionsResolver $ resolver)** – JohnPoe

+0

我覺得你忘了'使用Symfony \ Component \ OptionsResolver \ OptionsResolver;'在你的LocationType.php;) – Alsatian

+0

其實我沒有''inherit_data'=> true'解決了這個問題..但是我必須在父實體中創建getters&setters ..現在它工作:) ..謝謝兄弟;) – JohnPoe

0

首先,請注意如何將字段添加到PersonType表單中,因爲它將完全相同。

->add('email', TextType::class, array('label' => 'E-Mail', 'attr' => array('class' => 'form-control'))); 

你在這裏做什麼是添加子窗體TextType。它實際上包含單個字段,但它仍然是一種形式。

您可以將PersonType添加到任何其他表單中。這將是這樣的:

->add('person', PersonType::class, array(/* some options if needed*/); 

And how do I then set the submit-button?

正如Best Practices爲Symfony的形式提到的,我會建議他們添加模板,而不是表單對象。

+0

嗨,請給我更多的信息,怎麼做?當我嘗試它時,Symfony嘗試並找不到gettes/setters' - > add('location',LocationType :: class)' – JohnPoe

+0

呃,這裏很難具體說明,因爲你的問題很廣泛。確切的解決方案可能會因您的情況而異。我可以建議閱讀Symfony的* The Book * http://symfony.com/doc/current/book/forms表格。HTML和其他資源,如食譜或最佳做法等也可在Symfony的頁面上找到。然後你將獲得有關Symfony Forms中表單的基本知識,並且你將能夠做更多的事情,最終你可以在這裏提出一個更具體的問題。 –

+0

當沒有指定'inherit_data'=> true選項時,表單組件將您的字段視爲子節點的子節點。只有當你的實體包含這些孩子作爲嵌入類時纔有效。 http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/tutorials/embeddables.html與選項inherit_data,孩子們directrly綁定到你的人的實體。 – Alsatian