Symfony3中一个控制器中的多个表单

时间:2016-09-28 11:49:38

标签: symfony

我试图在一个控制器中使用两种形式。他们每个人都使用不同的实体。每当我尝试使用第二种形式时,第一种形式执行。怎么做对了?

    $document = new Document();

    $form2 = $this->createFormBuilder($document)
        ->add('file', FileType::class, array('label' => 'Wgraj plik '))
        ->add('name', TextType::class, array('label' => 'Nazwa dodawanego pliku'))
        ->add('toFill', CheckboxType::class, array('label' => 'Do wypełnienia?', 'required' => false))
        ->add('save', SubmitType::class, array('label' => 'Dodaj plik'))
        ->getForm();


    if ($form2->handleRequest($request)->isValid() && $form2->isSubmitted()) {
        $em = $this->getDoctrine()->getManager();

        $document->upload();
        $document->setFormId($id);

        $em->persist($document);
        $em->flush();


    }

    $raportFiles = new RaportFiles();

    $formRaportsInput = $this->createFormBuilder($raportFiles)
        ->add('file', FileType::class, array('label' => 'Wgraj plik '))
        ->add('name', TextType::class, array('label' => 'Nazwa dodawanego pliku'))
        ->add('save', SubmitType::class, array('label' => 'Dodaj plik'))
        ->getForm();


    if ($formRaportsInput->handleRequest($request)->isValid()) {
        $em = $this->getDoctrine()->getManager();

        $raportFiles->upload();
        $raportFiles->setFormId($id);

            $em->persist($raportFiles);
            $em->flush();
    }

我尝试使用$this->get('form.factory')->createNamedBuilder(),但我无法让它发挥作用。

2 个答案:

答案 0 :(得分:2)

  

@Cerad但是如何在symfony中做到这一点?

首先定义三条路线,一条使用GET显示两种形式。另外两个使用POST来处理单个表单。

http://symfony.com/doc/current/routing/requirements.html#adding-http-method-requirements

forms_show:
    path:     /forms
    defaults: { _controller: MyBundle:FormsController:show }
    methods:  [GET]
form_document_post:
    path:     /form-document
    defaults: { _controller: MyBundle:FormsController:documentPost }
    methods:  [POST]
form_raport__files_post:
    path:     /form-raport-files
    defaults: { _controller: MyBundle:FormsController:raportFilesPost }
    methods:  [POST]

您的控制器需要三种操作方法。让我们假设您已经制作了表单类型以保存一些输入。

http://symfony.com/doc/current/forms.html#creating-form-classes

class FormsController {
    public function showAction() {
        $document = new Document();
        $documentForm = $this->createForm(DocumentType::class,$document,array(
            'action' => $this->generateUrl('form_document_post')));

        $raportFiles = new RaportFiles();
        $raportFilesForm = $this->createForm(RaportFilesType::class,$raportFiles,array(
            'action' => $this->generateUrl('form_raport_files_post')));

        // Return the processed template
    }
    // Only gets called when the document form is posted
    public function documentPostAction(Request $request)
    {
        $document = new Document();
        $documentForm = $this>createForm(DocumentType::class,$document);
        $documentForm->handleRequest($document);
        if ($documentForm->isValid()) {
            // Persist
            return $this->redirectToRoute('forms_show');
        }
        // You will have to decide how you want to handle form errors
    }
    // Repeat for second form

答案 1 :(得分:0)

您可以通过执行以下操作来检查是否提交了特定表单:

$form2->handleRequest($request);    
if ( $form2->isSubmitted() && $form->isValid() ){
    ...
}

$formRaportsInput->handleRequest($request);     
if ( $formRaportsInput->isSubmitted() && $formRaportsInput->isValid() ){
    ... 
}

这将成为工作;)

编辑:当然,这是在同一个控制器上的行动!