2015-11-05 49 views
1

我希望嵌入式表單。所以存在有公司的客戶。在註冊時以單一形式,他必須描述你自己和他的公司。因此,我有:如何防止枝條自動呈現嵌入的表單?

namespace AppBundle\Form; 

use AppBundle\Entity\Company; 
use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 

class CompanyType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add(
       'type', 
       'choice', 
       [ 
        'choices' => Company::choices() 
       ] 
      ) 
      ->add('name', 'text') 
      ->add('licenseNumber', 'text') 
      ->add('country', 'country') 
      ->add('officeAddress', 'text') 
      ->add('registrationAddress', 'text') 
      ->add('phone', 'text') 
      ->add('fax', 'text') 
      ->add('email', 'email') 
      ->add('description', 'textarea') 
      ->add('Documents', 'text', [ 'compound' => true ]) 
     ; 

     $builder->setMethod('POST'); 
    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults([ 
      'data_class' => 'AppBundle\Entity\Company' 
     ]); 
    } 

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




namespace AppBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 

class RegisterType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('name', 'text') 
      ->add('email', 'email') 
      ->add(
       'password', 
       'repeated', 
       [ 
        'first_options' => [ 
         'label' => 'Password' 
        ], 
        'second_options' => [ 
         'label' => 'Repeat Password' 
        ], 
        'type'   => 'password', 
        'property_path' => 'rawPassword', 
       ] 
      ) 
      ->add('Companies', 'collection', ['type' => new CompanyType()]) 
      ->add('submit', 'submit') 
      ->setMethod('POST') 
     ; 
    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver 
      ->setDefaults(
       [ 
        'data_class' => 'AppBundle\Entity\Customer' 
       ] 
      ) 
     ; 
    } 

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

因此,在RegistreType我想嵌入CompanyType。因此,在我登記控制器我做的:

public function registerAction(Request $request) 
    { 
     if ($customer = $this->getUser()) { 
      return $this->redirectToRoute('_profile_index'); 
     } 

     $customerManager = $this->get('app.services.customer_manager'); 

     $customer = new Customer(); 
     $company = new Company(); 
     $customer->getCompanies()->add($company); 

     $form = $this->createForm(new RegisterType(), $customer); 

     $form->handleRequest($request); 

     if ($form->isSubmitted() && $form->isValid()) { 
      $customerManager->create($form->getData()); 

      //return $this->redirectToRoute('_security_register_confirmation'); 
     } 

     return $this->render(
      'AppBundle:Security:register.html.twig', 
      [ 
       'form' => $form->createView() 
      ] 
     ); 
    } 

在結果我recive嵌入公司形式,但它是全自動的渲染和你看到它是不好的。快樂幫助我。

enter image description here

回答

0

我明白了!那是因爲我不手動e.g處理表單字段:

{{ form_label(form.Companies[0].name) }}         
{{ form_errors(form.Companies[0].name) }} 
{{ form_widget(form.Companies[0].name) }} 
0

我不知道你是否瞭解Form themes in Symfony,但我認爲這可能是你在找什麼。

您必須在src \ YourBundle \ Resources \ views \ Form(或其他任何地方,但這是Symfony文檔指出的路徑)中創建表單主題。我們稱之爲registrationForm.html.twig。

內,您將創建一個自定義主題,爲您的personnal使用,設計公司的形式,你的願望,這種方式:

{% block _app_company_row %} // or {% block _app_company_widget %} 
    // your custom template here 
    // see documentation to learn how to do this correctly 
    // as this can be rather difficult to understand 
{% endblock %} 

然後,你將不得不「稱之爲」這一主題在你的模板:

{% form_theme form 'YourBundle:Form:registrationForm.html.twig' %} 

表格是包含formView實例的twig變量。

但我thinkthere可能是你的問題有些ohter問題,如「什麼是有選擇的形式組成部分?」 ..

有關公司實體的一些相關信息可以幫助太..