Php 设置选择字段Symfony FormType的默认值
我希望用户选择一种问卷类型,因此我设置了一个包含问卷类型的选择 类型从实体加载Php 设置选择字段Symfony FormType的默认值,php,symfony,Php,Symfony,我希望用户选择一种问卷类型,因此我设置了一个包含问卷类型的选择 类型从实体加载QuestionType $builder ->add('questionType', 'entity', array( 'class' => 'QuizmooQuestionnaireBundle:QuestionType', 'property' => 'questionTypeName',
QuestionType
$builder
->add('questionType', 'entity', array(
'class' => 'QuizmooQuestionnaireBundle:QuestionType',
'property' => 'questionTypeName',
'multiple' => false,
'label' => 'Question Type'))
->add('type', 'hidden')
;
我无法实现的是为结果选择设置默认值
我在谷歌上搜索了很多,但我只找到了一个只适用于数组的
class MyFormType extends AbstractType{
public function __construct($foo){
$this->foo = $foo;
}
$builder
->add('questionType', 'entity', array(
'class' => 'QuizmooQuestionnaireBundle:QuestionType',
'property' => 'questionTypeName',
'multiple' => false,
'label' => 'Question Type'
'data' => $this->foo))
->add('type', 'hidden')
;
}
内部控制器
$this->createForm(new MyFormType($foo));
如果使用实体结果创建选择菜单,则可以使用
首选选项将显示在列表的顶部,如文档所示,因此,如果您不添加空值,则第一个选项在技术上将是默认值。我通过在控制器的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
));
}
公共函数newAction($id)
{
$entity=new RankingQuestion();
//从数据库中获取值
$em=$this->getDoctrine()->getManager();
$type=$em->getRepository('quizmoosquestionnairendle:QuestionType')->findBy(数组('name'=>'Ranking Question'));
$entity->setQuestionType($type);//createForm(新RankingQuestionType(),$entity);
返回$this->render('QuizmooQuestionnaireBundle:RankingQuestion:new.html.twig',数组(
“实体”=>$entity,
'form'=>$form->createView(),
“id_调查问卷”=>$id
));
}
如果具有恒定的默认值(),则可以使用数据属性
但是,如果您正在使用表单编辑实体(而不是创建新实体),这将没有帮助。在模型中预先设置的公认答案是好的。但是,我遇到了这样一种情况:我需要集合中每个对象的某个字段的默认值。集合启用了allow\u add
和allow\u remove
选项,因此我无法预先实例化集合中的值,因为我不知道客户端将请求多少对象。因此,我使用了empty_data
选项和所需默认对象的主键,如下所示:
class MyChildType
extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('optionalField', 'entity', array(
'class' => 'MyBundle:MyEntity',
// Symfony appears to convert this ID into the entity correctly!
'empty_data' => MyEntity::DEFAULT_ID,
'required' => false,
));
}
}
class MyParentType
extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('children', 'collection', array(
'type' => new MyChildType(),
'allow_add' => true
'allow_delete' => true,
'prototype' => true, // client can add as many as it wants
));
}
}
在实体内的成员变量上设置默认值(QuestionType
),例如
谢谢你的回答,问题是“foo”必须来自数据库我更新了答案,但是你的解决方案似乎是更好的方法。你能给我更多的解释吗?因为正如我提到的,我已经看到了首选选项文档,但什么都没有。你是在输入首选选项的值还是显示的文本?据我所知,如果有帮助的话,应该是身份证。
/**
* default the numOfCourses to 10
*
* @var integer
*/
private $numCourses = 10;