我正在尝试使用Symfony为我的应用程序创建一个测验。 此时我有3个班级,Qcm,QcmQuestion和QcmAnswer。 我有多个问题,其中包含多个答案,我想将答案显示为单选按钮。
我只是将它们显示为输入。如何将它们显示为单选按钮?
BaseController.php
$em = $this->getDoctrine()->getManager()>getRepository('QcmBundle:QcmQuestion');
$qcmQuestions = $em->findBy(array('qcm' => $id));
$formBuilderQuestionnaire = $this->createFormBuilder();
$i = 0;
foreach ($qcmQuestions as $qcmQuestion) {
$formBuilder = $this->get('form.factory')->createNamedBuilder($i, FormType::class, $qcmQuestion);
$formBuilder
->add('question')
->add('qcmAnswers', CollectionType::class, [
'entry_type' => QcmAnswerType::class
])
;
$formBuilderQuestionnaire->add($formBuilder);
$i++;
}
$form = $formBuilderQuestionnaire->getForm();
$form->add('save', SubmitType::class, array('label' => 'Envoyer',
"attr" => array("class" => "btn btn-primary")));
return $this->render('QcmBundle:qcm:qcmQuestions.html.twig', ["qcmQuestions" => $qcmQuestions, "form" => $form->createView()]);
QcmAnswerType
class QcmAnswerType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('response');
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'QcmBundle\Entity\QcmAnswer'
));
}
public function getBlockPrefix()
{
return 'qcmbundle_qcmanswer';
}
答案 0 :(得分:0)
您不希望QcmAnswerType
+ EntityType
,但只希望multiple: false
选项expanded: true
和 ->add('qcmAnswers', CollectionType::class, [
'entry_type' => QcmAnswerType::class
])
。
所以而不是
->add('qcmAnswers', EntityType::class, [
'multiple' => false,
'expanded' => true,
])
尝试使用类似的东西:
String h = " { "
+ " \"script\": { \n"
+ " \"inline\": \"ctx._source.order=params.prasad\", \n "
+ "\"params\": { \"prasad\":["+column+"] \n"
+ " } , \n"
+ "\"lang\": \"painless\" \n"
+ "},\n"
+ "\"query\": { \n"
+ "\"term\": { \n"
+ "\"orgid\": "+orgId+" \n"
+ "} \n"
+ "}";
有关EnttiyType的更多信息,请参阅Symfony Docs。
答案 1 :(得分:0)
我设法解决了我的问题,我在EntityType中使用了查询。
Entities passed to the choice field must be managed. Maybe persist them in the entity manager?"
我遇到了这个错误,因为我需要将createNamedBuilder
这样的问题传递给 $formBuilder = $this->get('form.factory')->createNamedBuilder($i, FormType::class, $qcmQuestions);
:
$em = $this->getDoctrine()->getManager()->getRepository('QcmBundle:QcmQuestion');
$qcmQuestions = $em->findBy(array('qcm' => $id));
$formBuilderQuestionnaire = $this->createFormBuilder();
$i = 0;
foreach ($qcmQuestions as $qcmQuestion) {
/* @var $qcmQuestion QcmQuestion */
$formBuilder = $this->get('form.factory')->createNamedBuilder($i, FormType::class, $qcmQuestions);
$formBuilder
->add('qcmAnswers', EntityType::class, [
'class' => 'QcmBundle\Entity\QcmAnswer',
'expanded' => true,
'label' => $qcmQuestion->getQuestion(),
'query_builder' => function (EntityRepository $er) use ($qcmQuestion) {
return $er->createQueryBuilder('qcmAnswer')
->join('qcmAnswer.qcmQuestion', 'qcmQuestion')
->where('qcmAnswer.qcmQuestion = :qcmQuestionId')
->setParameter('qcmQuestionId', $qcmQuestion->getId());
},
]);
$formBuilderQuestionnaire->add($formBuilder);
$i++;
}
$form = $formBuilderQuestionnaire->getForm();
最终结果:
{{1}}