如何在symfony项目的同一个控制器中使用两个窗体?

gcxthw6b  于 2023-05-07  发布在  其他
关注(0)|答案(1)|浏览(114)

我想在我的symfony项目的同一个控制器'new_data'中使用两个表单,一个是主表单,另一个是次表单。我想在提交第二份表格时保持在同一页上。主表单应重定向到索引页,但secundary应添加一个列表中的数据,只有当用户提交第一个表单时才提交。
我改变了指令:重定向变成了评论。结果是一条错误消息,因为表单没有提交。
这样做的目的是,secundary表单在列表中添加数据,当用户准备就绪时,他通过提交主表单来验证该过程。
如果有人能帮助我,我将非常感激。
下面是我的控制器的代码:

public function new(Request $request, ArmyRepository $armyRepository, RegionRepository $regionRepository, NotificationRepository $notificationRepository): Response
{
    $army = new Army();
    $form = $this->createForm(ArmyType::class, $army);
    $form->handleRequest($request);
    $form2 = $this->createForm(SelectCUType::class, $army);
    $form2->handleRequest($request);
    $notifmenu = $notificationRepository->findBy(['statut'=>'non lu','addressee'=>$this->getUser()]);
    $regions = $regionRepository->findAll();
    $user = $this->getUser();

    if ($form->isSubmitted() && $form->isValid()) {
        $armyRepository->add($army, true);

        return $this->redirectToRoute('app_army_index', [], Response::HTTP_SEE_OTHER);
    }
    if ($form2->isSubmitted() && $form->isValid()) {
        $armyRepository->add($army, true);

        //return $this->redirectToRoute('app_army_index', [], Response::HTTP_SEE_OTHER);
    }

    return $this->renderForm('army/new.html.twig', [
        'army' => $army,
        'form2' => $form2,
        'form' => $form,
        'regions' => $regions,
        'user' => $user,
        'notifmenu'=>$notifmenu,
    ]);
}
woobm2wo

woobm2wo1#

当您处理次要表单时,将无法在新函数中找到主要表单,因为该项是由次要表单创建的,或者说需要创建该项
你可以在你的表单类型(和twig文件)中实现一个按钮来调用辅助控制器上的new()函数。这个过程应该是创建主项,这样你就可以在主项中添加辅助项,并在创建辅助项后重定向到主项的edit()函数,就像这样,但是你需要像主项的占位符一样填充所需的数据列。

// src/Controller/PrimaryController.php
public function new(Request $request, ArmyRepository $armyRepository, RegionRepository $regionRepository, NotificationRepository $notificationRepository): Response
{
    $army = new Army();
    $form = $this->createForm(ArmyType::class, $army);
    $form->handleRequest($request);
    $notifmenu = $notificationRepository->findBy(['statut'=>'non lu','addressee'=>$this->getUser()]);
    $regions = $regionRepository->findAll();
    $user = $this->getUser();

    if ($form->isSubmitted() && $form->isValid()) {
        $armyRepository->add($army, true);
        
        // new secondary item
        /** @var SubmitType $btnAddSecondaryStuff */
        $btnAddSecondaryStuff = $form->get('secondary_stuff_add_btn');

        if ($btnAddSecondaryStuff->isClicked()) {
            return $this->redirectToRoute('secondary_stuff_add_route', ['id' => $army->getId()], Response::HTTP_SEE_OTHER);
        }

        return $this->redirectToRoute('app_army_index', [], Response::HTTP_SEE_OTHER);
    }

    return $this->renderForm('army/new.html.twig', [
        'army' => $army,
        'form' => $form,
        'regions' => $regions,
        'user' => $user,
        'notifmenu'=>$notifmenu,
    ]);
}

添加添加按钮到编辑功能

// src/Controller/PrimaryController.php
public function edit(Request $request, Army $army, ArmyRepository $armyRepository, RegionRepository $regionRepository, NotificationRepository $notificationRepository): Response
{
    $form = $this->createForm(ArmyType::class, $army);
    $form->handleRequest($request);
    $notifmenu = $notificationRepository->findBy(['statut'=>'non lu','addressee'=>$this->getUser()]);
    $regions = $regionRepository->findAll();
    $user = $this->getUser();

    if ($form->isSubmitted() && $form->isValid()) {
        $armyRepository->add($army, true);

        // new secondary item
        /** @var SubmitType $btnAddSecondaryStuff */
        $btnAddSecondaryStuff = $form->get('secondary_stuff_add_btn');

        if ($btnAddSecondaryStuff->isClicked()) {
            return $this->redirectToRoute('secondary_stuff_add_route', ['id' => $army->getId()], Response::HTTP_SEE_OTHER);
        }

        // regular save
        return $this->redirectToRoute('app_army_index', [], Response::HTTP_SEE_OTHER);
    }

    $secondaryItems = $army->getSecondaryItems();

    return $this->renderForm('army/edit.html.twig', [
        'army' => $army,
        'secondary_items' => $secondaryItems,
        'form' => $form,
        'regions' => $regions,
        'user' => $user,
        'notifmenu'=>$notifmenu,
    ]);
}

在你的表单类型中你需要添加添加按钮

// src/Form/PrimaryFormType.php
public function buildForm(FormBuilderInterface $builder, array $options): void
{
    $builder
        ...
        ->add('secondary_stuff_add_btn', SubmitType::class, [
            'label' => 'add secondary item',
        ])
    ;
}

在你的wig文件里你可以放下一个现有的次要项目列表并添加添加按钮

// templates/army/_form.html.twig
{{ form_row(form.secondary_stuff_add_btn) }}

相关问题