0
我有一个非常简单的脚本。它从表中读取ID和NAME,并将它们作为选项添加到我的choiceType中。symfony 3 ChoiceType is not无效
已解决 查看上一个脚本。您必须再次在表单中添加所有选项。
这是行动。
/**
* @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
你呈现:'在树枝模板'form_widget(form._token)? –
@ĐuroMandinić这是我如何打印\t {{form_start(form_event)}} {{form_widget(form_event)}} {{form_end(form_event)}} –
您是否收到任何验证错误消息?你怎么知道这个领域是无效的? –