2 回答

TA貢獻1811條經驗 獲得超4個贊
您已經通過 ajax 請求(id參數)中的查詢字符串將參數傳遞給控制器,所以我假設您知道如何添加新參數,比如說type.
由于::class只返回一個具有完全限定類名的字符串,您可以使用這個新參數來構建您的FormType類并正常實例化它。如果請求的類型不存在,createForm將拋出InvalidArgumentException.
$ticketType = $request->query->get('type', ''); // Set 'main' type if not specified
$ticketFormType = 'App\Form\' . $ticketType . 'TicketType';
$ticket = new Ticket();
try {
$form = $this->createForm(TicketType::class, $ticket);
} catch (Symfony\Component\Form\Exception\InvalidArgumentException $e) {
// FormType doesn't exist
return new Response(null, 400);
}
return $this->render('ticket/select.html.twig', ['form' => $form->createView()]);
- 2 回答
- 0 關注
- 152 瀏覽
添加回答
舉報