我有一个非常简单的脚本。它从Table读取ID和NAME,并将它们作为选项添加到我的choiceType。
解决 见最后一个脚本。你必须再次在表格中添加所有选项。
这是行动。
/**
* @Route("/monitor/setup/carousel", name="SetupCarousel")
*/
public function SetupCarouselManageAction(Request $request){
$repository = $this->getDoctrine()->getRepository("AppBundle:Picture");
$pictures = $repository->findAll();
$p = array();
$p['-- Bitte Bild setzen'] = -1;
foreach($pictures as $pic){
$p[$pic->getTitle()] = $pic->getId();
}
$carousel = new Carousel();
$FormCarousel = $this->createForm(CarouselType::class, $carousel, array(
'pictures' => $p,
'action' => $this->generateUrl('SetupInputCarousel')
));
$render = $this->render('SetupCarousel.html.twig',array(
'form_event' => $FormCarousel->createView()
));
return $render;
}
表格类型:
class CarouselType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array $options){
$builder
->add("bildId", ChoiceType::class,
array('choices' => $options['pictures']))
->add('submit', SubmitType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setDefaults(array(
'data_class' => Carousel::class,
'pictures'=> array('array')))
->setAllowedTypes('pictures', array('array'))
;
}
}
到目前为止,这么好。在我的页面上获取ChoiceType Box,我可以选择我想要的条目并提交它。如果我想检查数据是否有效,则失败。
这是我的实体
class Carousel{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/** @ORM\Column(type="integer") */
protected $bildId;
/**@ORM\Column(type="string") */
protected $text;
这是我现在收回数据的方式。
/**
* @Route("/monitor/setup/input/carousel",name="SetupInputCarousel")
*/
public function SetupInputCarouselAction(Request $request){
$carousel = new Carousel();
$form = $this->createForm(CarouselType::class,$carousel);
$form->handleRequest($request);
if($form->isSubmitted()){
//pease rework this in future
$data = $request->request->get('carousel');
$carousel->setBildId($data['bildId']);
$carousel->setText($data['text']);
if($carousel->getBildId() == -1){
return $this->redirectToRoute('SetupHome');
}
$em = $this->getDoctrine()->getManager();
$em->persist($carousel);
$em->flush();
return $this->redirectToRoute('SetupCarousel');
}
有任何想法,为什么以下脚本不起作用?这是我在项目中所有其他页面上可以做到的方式。
public function SetupInputEventsAction(Request $request){
//YOU HAVE TO ADD THE ORIGINAL OPTIONS, THIS SOLVE THE PROBLEM
$repository = $this->getDoctrine()->getRepository("AppBundle:Picture");
$pictures = $repository->findAll();
$p = array();
$p['-- Bitte Bild setzen'] = -1;
foreach($pictures as $pic){
/** @var pic Picture */
$p[$pic->getTitle()] = $pic->getId();
}
$carousel = new Carousel();
$form = $this->createForm(CarouselType::class,$carousel, array(
'pictures' => $p,
'action' => $this->generateUrl('SetupInputCarousel')
));
/*
SEE WORKING ONE ABOVE
$event = new Carousel();
$form = $this->createForm(CarouselType::class,$event);*/
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
//handel type
$em = $this->getDoctrine()->getManager();
$em->persist($event);
$em->flush();
}
return $this->redirectToRoute('SetupEvents');
}
友好的问候,
Fabian Harmsen
答案 0 :(得分:0)
Soluion很容易,您必须将所有选项添加到" SetupInputEventsAction"功能。
public function SetupInputEventsAction(Request $request){
//YOU HAVE TO ADD THE ORIGINAL OPTIONS, THIS SOLVE THE PROBLEM
$repository = $this->getDoctrine()->getRepository("AppBundle:Picture");
$pictures = $repository->findAll();
$p = array();
$p['-- Bitte Bild setzen'] = -1;
foreach($pictures as $pic){
/** @var pic Picture */
$p[$pic->getTitle()] = $pic->getId();
}
$carousel = new Carousel();
$form = $this->createForm(CarouselType::class,$carousel, array(
'pictures' => $p,
'action' => $this->generateUrl('SetupInputCarousel')
));
/*
SEE WORKING ONE ABOVE
$event = new Carousel();
$form = $this->createForm(CarouselType::class,$event);*/
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
//handel type
$em = $this->getDoctrine()->getManager();
$em->persist($event);
$em->flush();
}
return $this->redirectToRoute('SetupEvents');
}
友好的问候,
Fabian Harmsen