1
我直接在我的控制器動作所產生的一種形式:Symfony2的:獲取後DATAS與單選按鈕
$repoQuestions = $this->getDoctrine()->getRepository('QuizzBundle:Questions');
$questions = $repoQuestions->findBy(array('quizz'=>$quizz));
$formBuilder = $this->createFormBuilder();
foreach($questions as $q){
$formBuilder
->add('rep'.$q->getId(), 'entity', array(
'label'=>$q->getTitre(),
'property' => 'titre',
'query_builder' => function(\MyApp\QuizzBundle\Entity\ReponsesRepository $r) use($q){
return $r->getReponsesByQuestion($q);
},
'multiple'=>false,
'expanded'=>true,
'required'=>true
));
}
$formQuizz = $formBuilder->getForm();
if ($request->getMethod() == 'POST') {
$formQuizz->bind($request);
$data = $formQuizz->getData();
if ($formQuizz->isValid()) {
$em = $this->getDoctrine()->getManager();
$repoRep = $this->getDoctrine()->getRepository('QuizzBundle:Reponses');
return $this->render('QuizzBundle:Quizz:resultat-quizz.html.twig', array(
'data'=>$data
));
}
}
在我看來
我有:
<form method="POST" action="{{path('quizz_quizz', {'cat':quizz.categorie.nom, 'id':quizz.id})}}" {{form_enctype(formQuizz)}} id='formQuizz' novalidate>
{% for q in questions %}
<div id="question">
<img src="{{asset('bundles/site/images/quizz-' ~quizz.id~ '/'~q.slug~'.png')}}"><br>
{{form_label(attribute(formQuizz, 'rep' ~ q.id)) }}<br>
{{form_widget(attribute(formQuizz, 'rep' ~ q.id))}}
</div>
{% endfor %}
{{ form_rest(formQuizz) }}
</form>
但是當我提出我的表格功能的isValid ()是正確的,但我不能讓我的形式DATAS ...
當我嘗試:
$data = $formQuizz->getData();
print_r($data);
我得到:
陣列([REP11] => [rep12] => [rep13] => [rep14] => [rep15] => [rep16] => [rep17] => [rep18] => [rep19] => [rep20] =>)