简体   繁体   English

Symfony3中的一个控制器中有多种形式

[英]Multiple forms in one controller in Symfony3

I'm trying to use two forms in one controller. 我正在尝试在一个控制器中使用两种形式。 Each of them uses diffrent entity. 他们每个人都使用不同的实体。 Everytime i try to use second form, the first one executes. 每当我尝试使用第二种形式时,第一种形式就会执行。 How to make it right? 如何使它正确?

    $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();
    }

I tried using $this->get('form.factory')->createNamedBuilder() instead, but i can't get it to work. 我尝试使用$this->get('form.factory')->createNamedBuilder()代替,但是我无法使其正常工作。

@Cerad but how to do this in symfony? @Cerad但是如何在symfony中做到这一点?

Start by defining three routes, one uses GET to display both forms. 首先定义三个路线,一个使用GET显示两种形式。 The other two uses POST to process an individual form. 另外两个使用POST处理单个表单。

http://symfony.com/doc/current/routing/requirements.html#adding-http-method-requirements 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]

Your controller needs three action methods. 您的控制器需要三种操作方法。 Lets assume you have made form types just to save some typing. 假设您已创建表单类型只是为了保存一些键入内容。

http://symfony.com/doc/current/forms.html#creating-form-classes 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

You can check if a specific form is submitted by doing this : 您可以通过以下操作检查是否提交了特定表格:

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

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

This will make the job ;) 这将使工作;)

EDIT : And this is on the same controller action of course ! 编辑:这当然是在同一控制器上!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM