Generate same form type on same page multiple times Symfony2

后端 未结 2 1864
离开以前
离开以前 2020-12-16 07:08

I\'m trying to generate a form type in particular the \"ProductLanguageType\".

I want to generate the ProductLanguageType as many times as the current numbers of exi

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-16 07:36

    Now in Symfony 3.0 they changed the createNamedBuilder, so it's possible to solve this by only calling:

    use AppBundle\Form\ShippingTrackCodeReturnType;
    
    $uniqueForm = $this->get('form.factory')->createNamedBuilder('ship_form_'.$orderRecord->getId(), ShippingTrackCodeReturnType::class, $orderRecord)->getForm();
    

    So, you just need to loop to display and save them:

    foreach ($invoice->getOrderRecords() as $key => $orderRecord) 
    {
        // creates the forms with different names
        $returnShipTrackCodeForm = $this->get('form.factory')->createNamedBuilder('ship_form_'.$orderRecord->getId(), ShippingTrackCodeReturnType::class, $orderRecord)->getForm();
    
        $returnShipTrackCodeForm->handleRequest($request);
        if ($returnShipTrackCodeForm->isSubmitted() && $returnShipTrackCodeForm->isValid()) 
        {
            // flush object
        }
    
        $returnForms[$orderRecord->getId()] = $returnShipTrackCodeForm;
    }  
    

提交回复
热议问题