symfony setData()错误

时间:2018-04-06 22:55:03

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

我正在尝试在formbuilder创建的表单中放置一个值,我从请求获取另一个twig文件的值,我正在尝试使用$ form-> setData(array('field'=> ;值));

代码控制器:

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的数据,但它的问题相同,“表格无效”

3 个答案:

答案 0 :(得分:1)

$form->get('hotel')->setData('John');
$form->get('match')->setData($match);

答案 1 :(得分:0)

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

答案 2 :(得分:-1)

我做了这个和它的工作,但我认为这很奇怪...无论如何,感谢大家回复我。

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");
    }