0
私は非常に簡単なスクリプトを持っています。テーブルからIDとNAMEを読み取り、それらをオプションとしてmy choiceTypeに追加します。symfony 3 ChoiceType ist Valid
SOLVED 最後のスクリプトを参照してください。フォームにすべてのオプションを再度追加する必要があります。
これがアクションです。
/**
* @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');
}
誰もが任意のアイデアを持っていますが、なぜこの次のスクリプトがうまくいかないのですか?これは私のプロジェクトの他のすべてのページでもできます。 Soluionは簡単です
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');
}
フレンドリーよろしく、
ファビアンHarmsen
あなたがレンダリングしました: '小枝テンプレートの' form_widget(form._tokenを)? –
@ĐuroMandinićこれは、フォームを印刷する方法です。\t {{form_start(form_event)}} {{form_widget(form_event)}} {{form_end(form_event)}} –
すべての検証エラーメッセージが表示されますか?フィールドが有効でないことをどのように知っていますか? –