简体   繁体   中英

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. See page for reference .

So basically, my Ajax calls /ticket/owner-select to get the owner options. 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.

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. 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).

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. 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 .

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. In case the requested type doesn't exist, createForm will throw 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()]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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