Php symfony setData()错误
我试图在formbuilder创建的表单中放置一个值,我通过请求从另一个细枝文件中获取该值,并尝试使用$form->setData(array('field'=>value)) 代码控制器:Php symfony setData()错误,php,forms,symfony,doctrine-orm,symfony-forms,Php,Forms,Symfony,Doctrine Orm,Symfony Forms,我试图在formbuilder创建的表单中放置一个值,我通过请求从另一个细枝文件中获取该值,并尝试使用$form->setData(array('field'=>value)) 代码控制器: public function ModifierGestionMatchAction(Request $request) { $id = $request->get('id'); $idmatch = $request->get('idm'); $em = $this-&
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);
}
注意:hotel和match是两个实体,我对match的setData()有问题,我试图设置idmatch的数据,但问题是相同的,“表单无效”我这样做了,而且工作正常,但我觉得很奇怪。。。无论如何,谢谢大家的回复
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()$类型->setHotel($hotel)$类型->设置匹配($match);然后persist and flush$type
您的表单生成器未设置为处理这些字段中的实体。您需要使用更改表单生成器,或者为其提供$match->getId()
,但是,如果您的MainBundle:reservationgestock::setMatch
也需要一个实体,那么$match->getId()
将不起作用,因为Symfony表单在发出handleRequest
时使用您的模型设置器
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");
}