【问题标题】:symfony 3 ChoiceType ist not Validsymfony 3 ChoiceType is not Valid
【发布时间】:2017-04-11 08:44:34
【问题描述】:

我有一个非常简单的脚本。它从 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 框,我可以选择我想要的条目并提交它。如果我想检查数据是否有效,它会失败。

这是我的实体

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

友好的问候,

法比安·哈姆森

【问题讨论】:

  • 您是否在树枝模板中渲染:form_widget(form._token)
  • @ĐuroMandinić 这就是我打印表单的方式 {{ form_start(form_event) }} {{ form_widget(form_event) }} {{ form_end(form_event) }}
  • 您是否收到任何验证错误消息?你怎么知道该字段无效?
  • @ĐuroMandinić 是的,有错误。 dl.lfs96.de/SymfonyProfiler_2017-04-12.htm 在这里你可以看到分析器。我已停止重定向,因此您可以忽略错误
  • @ĐuroMandinić 发现并解决了问题

标签: php forms symfony symfony-3.2


【解决方案1】:

解决方案很简单,您必须将所有选项添加到“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');
}

友好的问候,

法比安·哈姆森

【讨论】:

    猜你喜欢
    • 2022-09-25
    • 2017-12-03
    • 2018-03-29
    • 2017-03-03
    • 1970-01-01
    • 2012-09-15
    • 2021-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多