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

时间:2013-04-03 00:26:12

标签: php symfony

我希望用户选择一种问卷类型,因此我设置了一个包含问卷类型的选择。

类型是从实体QuestionType加载的。

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

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

我搜索了很多,但我只得到了preferred_choice solution,它只适用于数组

5 个答案:

答案 0 :(得分:3)

我是通过在我的控制器的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属性 但如果您使用表单编辑实体(而不是创建新实体),它将无济于事

答案 1 :(得分:2)

如果您使用实体结果创建选择菜单,则可以使用preferred_choices

首选的选项将在文档中显示的列表顶部呈现,因此第一个选项在技术上将是默认值,前提是您不添加空值。

答案 2 :(得分:1)

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

答案 3 :(得分:1)

预先在模型中设置的答案是好的。但是,我遇到了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
        ));
    }
}

答案 4 :(得分:0)

在您的实体(QuestionType)内的成员变量上设置默认值,例如

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