2013-04-03 120 views
1

我希望用戶選擇一種問卷類型,因此我設置了一個包含問卷類型的選擇。設置選擇字段的默認值Symfony FormType

類型從實體QuestionType加載。

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

無法實現的是將默認值設置爲結果選擇。

我用Google搜索了很多,但我只拿到了preferred_choice solution只使用數組

回答

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

您可以使用data屬性,如果你有一個不變的缺省值(http://symfony.com/doc/current/reference/forms/types/form.html) ,但它不會是有益的,如果你使用的是表格編輯實體(不創建一個新的)

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

感謝您的回答,問題是「富」有來形成數據庫 – zizoujab

+1

我的答案更新,但您的解決方案似乎是一個更好的方式去。 –

2

如果您使用的是實體結果來創建一個選擇菜單,然後就可以工作使用preferred_choices

首選項將顯示在列表頂部,就像它在文檔中所說的一樣,所以首先在技術上將默認爲默認值,但不會添加空值。

+0

您能否提供更多解釋,因爲正如我所提到的,我已經看到了首選項文檔,但什麼也沒有收到 – zizoujab

+0

您是將首選項的值還是顯示的文本?從我所能看到的將是id,如果這有幫助的話。 – qooplmao

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

設置你的實體(QuestionType),例如內部的成員變量的默認值

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