Forms 如何防止细枝自动渲染嵌入表单?

Forms 如何防止细枝自动渲染嵌入表单?,forms,symfony,twig,embedding,Forms,Symfony,Twig,Embedding,我不想填表格。有公司的客户也是如此。在注册期间,他必须以单一形式描述你自己和他的公司。因此,我有: namespace AppBundle\Form; use AppBundle\Entity\Company; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver

我不想填表格。有公司的客户也是如此。在注册期间,他必须以单一形式描述你自己和他的公司。因此,我有:

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()
            ]
        );
    }
结果,我收到了嵌入的公司表单,但它是自动渲染的,正如您所看到的,它并不好。请帮帮我


我明白了!这是因为我不手动处理表单字段,例如:

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

我明白了!这是因为我不手动处理表单字段,例如:

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

我不知道你是否知道,但我想这可能就是你想要的

您必须在src\YourBundle\Resources\views\form(或其他任何地方)中创建一个表单主题,但这是Symfony文档所指示的路径。我们称之为registrationForm.html.twig

在内部,您将创建一个供个人使用的自定义主题,按照您的意愿设计公司表单,方法如下:

{% 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实例的细枝变量

但我认为在你的问题中可能还有其他一些问题,比如“什么是选择表单组件”


一些关于公司实体的信息也会有所帮助。

我不知道你是否知道,但我想这可能是你想要的

您必须在src\YourBundle\Resources\views\form(或其他任何地方)中创建一个表单主题,但这是Symfony文档所指示的路径。我们称之为registrationForm.html.twig

在内部,您将创建一个供个人使用的自定义主题,按照您的意愿设计公司表单,方法如下:

{% 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实例的细枝变量

但我认为在你的问题中可能还有其他一些问题,比如“什么是选择表单组件”

一些关于公司实体的信息也会有所帮助