【问题标题】:symfony setData() errorsymfony setData() 错误
【发布时间】:2018-04-06 22:55:02
【问题描述】:

我正在尝试将值放入由 formbuilder 创建的表单中,我通过请求从另一个树枝文件中获取了值,并且我正在尝试使用 $form->setData(array('field'=>value ));

代码控制器:

public function ModifierGestionMatchAction(Request $request)
{
    $id = $request->get('id');
    $idmatch = $request->get('idm');
    $em = $this->getDoctrine()->getManager();
    $type = $em->getRepository("MainBundle:ReservationGestionStock")
        ->find($id);
    $match = $em->getRepository("MainBundle:Match")
        ->findOneByid($idmatch);
    $form = $this->createForm(ReservationGestionStockType::class, $type);
    $form->setData(array('hotel'=>null));
    $form->setData(array('match'=>$match));
    $form->handleRequest($request);
        if($form->isValid()){
        $em->persist($type);
        $em->flush();
        return $this->redirectToRoute("ReservationGestionStockAfficher");
    }

    return $this->render('MainBundle:GestionStock:GestionStockModifierMatch.html.twig',
        array(
            "form" => $form->createView(),
            "match" => $match
        ));
}

我的表单代码:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('hotel',HiddenType::class)
        ->add('match',HiddenType::class)
        ->add('nb')
        ->add('nbr')
        ->add('Valider', SubmitType::class)
        ->add('Reset', ResetType::class);
}

注意:酒店和匹配是 2 个实体,我遇到匹配的 setData() 问题,我尝试设置 idmatch 的数据但同样的问题,“表格无效”

【问题讨论】:

  • 您的表单构建器未设置为处理这些字段中的实体。您需要使用EntityType field 更改表单构建器,或者提供$match->getId(),但是如果您的MainBundle:ReservationGestionStock::setMatch 也需要一个实体,$match->getId() 将不起作用,因为 Symfony 表单使用您的模型设置器当发出handleRequest

标签: php forms symfony doctrine-orm symfony-forms


【解决方案1】:
$form->get('hotel')->setData('John');
$form->get('match')->setData($match);

【讨论】:

    【解决方案2】:

    提交后就这样做, $type = $form->getData(); $type->setHotel($hotel); $type->setMatch($match); then persist and flush $type

    【讨论】:

      【解决方案3】:

      我做了这个并且它的工作,但我觉得这很奇怪......无论如何感谢大家回复我。

      if ($form->isSubmitted()) {
              $gestion= new ReservationGestionStock();
              $gestion->setMatch(null);
              $gestion->setNb($form->getData()->getNb('nb'));
              $gestion->setNbr($form->getData()->getNbr('nbr'));
              $gestion->setHotel($hotel);
              $em->persist($hotel);
              $em->flush();
              return $this->redirectToRoute("ReservationGestionStockAfficher");
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-22
        • 2018-02-19
        • 2015-12-11
        • 2012-10-12
        相关资源
        最近更新 更多