简体   繁体   English

Symfony:从变量获取FormType名称

[英]Symfony: Get FormType Name From Variable

In my project, when I change the select, a ajax call gets a new select element and replaces it with my current one. 在我的项目中,当我更改选择时,ajax调用将获得一个新的选择元素,并将其替换为当前元素。 See page for reference . 参见页面以供参考

So basically, my Ajax calls /ticket/owner-select to get the owner options. 因此,基本上,我的Ajax调用/ticket/owner-select来获取所有者选项。 Everything works perfectly. 一切正常。

This is my controller for the url: 这是我的网址控制器:

//TicketController.php

...

/**
 * @Route("/ticket/owner-select", name="app_ticket_owner_select", methods={"GET"}, condition="request.isXmlHttpRequest()")
 */
public function getTicketOwnerSelect(Request $request, DepartmentRepository $departmentRepository)
{
    $department = $departmentRepository->findOneBy(['id' => $request->query->get('value')]); //gets id from get-parameter

    if(!$department) {
        return new Response(null, 204); //return empty response if no department selected or found
    }

    $ticket = new Ticket();
    $ticket->setDepartment($department);
    $form = $this->createForm(TicketType::class, $ticket);

    if(!$form->has('owner')) {
        return new Response(null, 204); //return empty response
    }

    return $this->render('ticket/select.html.twig', ['form' => $form->createView()]); //render the select element with correct options
}

...

Now I want to reuse the exact same url for other FormTypes, since I have multiple forms where the owner changes depending on another select field. 现在,我想为其他 FormType重用完全相同的url,因为我有多个表单,其中所有者根据另一个选择字段进行更改。

Example: 例:

$form = $this->createForm(TicketType::class, $ticket);
$form = $this->createForm(AnotherTicketType::class, $ticket);
$form = $this->createForm(AnotherAnotherTicketType::class, $ticket);

So the FormType should be dynamic. 因此,FormType应该是动态的。 Probably the best way would be another get parameter, but I'm not quite sure how to do that and especially check if that type even exists (error handling). 最好的方法可能是另一个get参数,但是我不太确定该怎么做,尤其是检查该类型是否存在(错误处理)。

Reason : The controller would look the same for every select-field on every ticket form. 原因 :每种票证表单上的每个选择字段的控制器外观都相同。 Since I do not want duplicated code (most part), I want to create a dynamic solution. 由于我不想重复代码(大部分),因此我想创建一个动态解决方案。

Mabye someone could help me. Mabye有人可以帮助我。 Thanks in advance. 提前致谢。

该类型的隐藏字段呢?

You are already passing parameters to the controller via query string in your ajax request (the id parameter) so I assume you know how to add a new parameter, let's say type . 您已经通过ajax请求中的查询字符串( id参数)将参数传递给控制器​​,因此我假设您知道如何添加新参数,例如type

Since ::class just returns a string with the fully qualified class name, you can just use this new parameter to build your FormType class and instance it normally. 由于::class仅返回具有完全限定的类名的字符串,因此您可以使用此新参数来构建FormType类并正常对其进行实例化。 In case the requested type doesn't exist, createForm will throw InvalidArgumentException . 如果请求的类型不存在, 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()]);

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

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