设置选择字段Symfony FormType的默认值


Set Default value of choice field Symfony FormType

我想让用户选择一种类型的问卷,所以我设置了一个select,其中包含问卷类型。

类型从一个实体QuestionType加载。

 $builder
        ->add('questionType', 'entity', array(
              'class'    => 'QuizmooQuestionnaireBundle:QuestionType',
              'property' => 'questionTypeName',
              'multiple' => false,
              'label' => 'Question Type'))
        ->add('type', 'hidden')
    ;

我无法实现的是为结果选择设置默认值。

我已经谷歌了很多,但我只有preferred_choice解决方案,它只适用于数组

我通过在我的控制器的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
    ));
}

如果你有一个固定的默认值(http://symfony.com/doc/current/reference/forms/types/form.html),你可以使用data属性。但它不会有帮助,如果你正在使用的形式来编辑实体(而不是创建一个新的)

如果你使用实体结果来创建一个选择菜单,那么你可以使用preferred_choices

首选项将呈现在列表的顶部,因为它在文档中说,所以第一个将是技术上的默认值,如果你不添加一个空值。

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));

事先在模型中设置的公认答案是好的。然而,我遇到了一种情况,我需要为collection类型中的每个对象的特定字段设置默认值。集合启用了allow_addallow_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)内部的成员变量设置一个默认值,例如

/**
 * default the numOfCourses to 10
 *
 * @var integer
 */
private $numCourses = 10;