symfony setData() 错误
symfony setData() error
我正在尝试将一个值放入由 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 的数据,但还是同样的问题,"表格无效"
$form->get('hotel')->setData('John');
$form->get('match')->setData($match);
我做了这个并且它起作用了,但我觉得它太奇怪了......无论如何感谢大家回复我。
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");
}
提交后就这样做,
$type = $form->getData(); $type->setHotel($hotel); $type->setMatch($match); then persist and flush $type
我正在尝试将一个值放入由 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 的数据,但还是同样的问题,"表格无效"
$form->get('hotel')->setData('John');
$form->get('match')->setData($match);
我做了这个并且它起作用了,但我觉得它太奇怪了......无论如何感谢大家回复我。
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");
}
提交后就这样做,
$type = $form->getData(); $type->setHotel($hotel); $type->setMatch($match); then persist and flush $type