我希望用户选择一种调查问卷类型,因此我设置了一个包含调查问卷类型的选择。
类型是从实体加载的QuestionType
.
$builder
->add('questionType', 'entity', array(
'class' => 'QuizmooQuestionnaireBundle:QuestionType',
'property' => 'questionTypeName',
'multiple' => false,
'label' => 'Question Type'))
->add('type', 'hidden')
;
我无法实现的是为结果选择设置默认值。
我用谷歌搜索了很多,但我只得到了首选解决方案仅适用于数组
我通过在控制器的 newAction 中设置类型来实现它,我会将设置的类型作为默认值。
public function newAction($id)
{
$entity = new RankingQuestion();
//getting values form database
$em = $this->getDoctrine()->getManager();
$type = $em->getRepository('QuizmooQuestionnaireBundle:QuestionType')->findBy(array('name'=>'Ranking Question'));
$entity->setQuestionType($type); // <- default value is set here
// Now in this form the default value for the select input will be 'Ranking Question'
$form = $this->createForm(new RankingQuestionType(), $entity);
return $this->render('QuizmooQuestionnaireBundle:RankingQuestion:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
'id_questionnaire' =>$id
));
}
您可以使用data
属性,如果您有一个恒定的默认值(http://symfony.com/doc/current/reference/forms/types/form.html)
但如果您使用表单来编辑实体(而不是创建新实体),这将没有帮助
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)