【问题标题】:Set entity parameter before form validation在表单验证之前设置实体参数
【发布时间】:2017-08-27 00:42:46
【问题描述】:

[设置]

  • Symfony 3
  • BoxEntity:[id,名称]
  • CandyEntity:[id, name]

[问题]

目前,在创建新糖果时,我必须选择一个盒子作为父实体。
问题是,我希望这个选择是自动化的。
盒子已经在数据库中注册,会话持有当前盒子参数以便轻松找回。
但是一旦数据发布,我不知道如何将其应用于糖果实体。

[文件]

AppBundle/Controller/CandyController.php

public function newAction(Request $request) {
    $$candy= new Candy();
    $form = $this->createForm('AppBundle\Form\CandyType', $conference);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($candy);
        $em->flush();

        return $this->redirectToRoute('candy_show', array('id' => $candy->getId()));
    }

    return $this->render('candy/new.html.twig', array(
        'candy' => $candy,
        'form' => $form->createView(),
    ));
}

AppBundle/Form/CandyType.php

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('nom')
            ->add('box'); //Remove from form, and set manually
}

我做了read this page,但不知道如何正确操作。
如果有人好心给我一个完整的例子来解决我的问题,将不胜感激。

【问题讨论】:

  • 您是将它连接到数据库中的现有框,还是创建一个新框,但已经在会话中设置了参数?会话本身存储了什么?
  • @JasonRoman 该框已存在于数据库中。会话保存盒子数据(id、name)。编辑了我的帖子以添加该详细信息... ;)

标签: php symfony symfony-forms


【解决方案1】:

您有多种选择来执行您想要的操作。您可以在表单提交后设置值:

public function newAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();

    $candy = new Candy();
    $box = $em->find('AppBundle\Entity\Box', $this->get('session')->get('boxId'));

    $form = $this->createForm('AppBundle\Form\CandyType', $candy);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        // add the box entity to the candy
        $candy->setBox($box);

        $em->persist($candy);
        $em->flush();

        return $this->redirectToRoute('candy_show', array('id' => $candy->getId()));
    }

    return $this->render('candy/new.html.twig', array(
        'candy' => $candy,
        'form' => $form->createView(),
    ));
}

您可以在将其传递给 createForm() 调用之前将其设置在 Candy 实体上,尽管在执行 handleRequest() 调用后它可能不会留在实体上:

    $em = $this->getDoctrine()->getManager();

    $candy = new Candy();
    $box = $em->find('AppBundle\Entity\Box', $this->get('session')->get('boxId'));

    $candy->setBox($box);

    $form = $this->createForm('AppBundle\Form\CandyType', $candy);
    $form->handleRequest($request);

您可以在表单事件中以您尝试的方式进行操作。您想要做的是将实体管理器和会话注入您的表单并将您的表单视为服务:

public function CandyType extends AbstractType
{
    private $em;
    private $session;

    public function __construct(EntityManager $em, SessionInterface $session)
    {
        $this->session = $session;
        $this->em      = $em;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // ... build the form

        $builder->addEventListener(
            FormEvents::PRE_SET_DATA,
            function (FormEvent $event) {
                $form = $event->getForm();

                $candy = $event->getData();
                $box = $this->em->find('AppBundle\Entity\Box', $this->session->get('boxId');

                $candy->setBox($box);
            }
        );
    }
}

您可能需要在 POST_SET_DATA 或 POST_SUBMIT 事件上执行此操作,但我不确定。我还在控制器中使用了$this->get('session'),但根据您的 Symfony 版本(> 3.3),您也可以将其作为服务注入到您的控制器中。

无论哪种方式,主要概念是使用 Doctrine 使用会话中存储的盒子 ID 从会话本身中获取您的 Box 实体,然后将其设置在您的 Candy 实体上。您甚至可以使用hidden 字段来获得相同的结果。正如我之前所说,有很多方法可以解决您的问题。

【讨论】:

  • 您的第一个解决方案正在运行。虽然我有一个警告说“除了 \AppBundle\Entity\Box,得到了对象”。这意味着缺少一些调整。
  • 这很奇怪 - 虽然没有看到实体定义,但很难判断问题出在哪里......或者你在哪一行得到了错误。
  • 这里是一个快速截图(我稍后会删除它)dropbox.com/s/5c912q07tmommk4/screenshot.png?dl=0
  • 啊。那是 PhpStorm 吗?看起来 IDE 正在努力知道您选择了一个 Salon 对象,但是当您运行它时不应该导致实际错误。
猜你喜欢
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-22
  • 1970-01-01
相关资源
最近更新 更多