2014-10-01 93 views
0

我有一個實體ChoiceQuestion,它有一個字段選項。 選項被定義爲一個數組。Symfony2來自實體字段的選擇

<?php 

namespace Survey\SurveyBundle\Entity; 

use Survey\SurveyBundle\Entity\BaseQuestion; 
use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity 
* @author GKLESSE 
* 
*/ 
class RadioQuestion extends BaseQuestion { 
    const TYPE = 'radio'; 
    /** 
    /* @ORM\Column(type="array") 
    */ 
    protected $options; 
    /** 
    * @var integer 
    */ 
    protected $id; 

    /** 
    * @var string 
    */ 
    protected $question; 

    /** 
    * @var integer 
    */ 
    protected $questionOrder; 

    /** 
    * @var \Survey\SurveyBundle\Entity\Survey 
    */ 
    protected $survey; 

    public function __construct(){ 
     $this->questiontype = self::TYPE; 
    } 

    /** 
    * Set options 
    * 
    * @param array $options 
    * @return RadioQuestion 
    */ 
    public function setOptions($options) 
    { 
     $this->options = $options; 

     return $this; 
    } 

    /** 
    * Get options 
    * 
    * @return array 
    */ 
    public function getOptions() 
    { 
     return $this->options; 
    } 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set question 
    * 
    * @param string $question 
    * @return RadioQuestion 
    */ 
    public function setQuestion($question) 
    { 
     $this->question = $question; 

     return $this; 
    } 

    /** 
    * Get question 
    * 
    * @return string 
    */ 
    public function getQuestion() 
    { 
     return $this->question; 
    } 

    /** 
    * Set questionOrder 
    * 
    * @param integer $questionOrder 
    * @return RadioQuestion 
    */ 
    public function setQuestionOrder($questionOrder) 
    { 
     $this->questionOrder = $questionOrder; 

     return $this; 
    } 

    /** 
    * Get questionOrder 
    * 
    * @return integer 
    */ 
    public function getQuestionOrder() 
    { 
     return $this->questionOrder; 
    } 

    /** 
    * Set survey 
    * 
    * @param \Survey\SurveyBundle\Entity\Survey $survey 
    * @return RadioQuestion 
    */ 
    public function setSurvey(\Survey\SurveyBundle\Entity\Survey $survey = null) 
    { 
     $this->survey = $survey; 

     return $this; 
    } 

    /** 
    * Get survey 
    * 
    * @return \Survey\SurveyBundle\Entity\Survey 
    */ 
    public function getSurvey() 
    { 
     return $this->survey; 
    } 
    /** 
    * @var string 
    */ 
    protected $questiontype; 


    /** 
    * Set questiontype 
    * 
    * @param string $questiontype 
    * @return RadioQuestion 
    */ 
    public function setQuestiontype($questiontype) 
    { 
     $this->questiontype = $questiontype; 

     return $this; 
    } 

    /** 
    * Get questiontype 
    * 
    * @return string 
    */ 
    public function getQuestiontype() 
    { 
     return $this->questiontype; 
    } 
    /** 
    * @var \Doctrine\Common\Collections\Collection 
    */ 
    protected $answers; 


    /** 
    * Add answers 
    * 
    * @param \Survey\SurveyBundle\Entity\Answer $answers 
    * @return RadioQuestion 
    */ 
    public function addAnswer(\Survey\SurveyBundle\Entity\Answer $answers) 
    { 
     $answers->setQuestion($this); 
     $this->answers[] = $answers; 

     return $this; 
    } 

    /** 
    * Remove answers 
    * 
    * @param \Survey\SurveyBundle\Entity\Answer $answers 
    */ 
    public function removeAnswer(\Survey\SurveyBundle\Entity\Answer $answers) 
    { 
     $this->answers->removeElement($answers); 
    } 

    /** 
    * Get answers 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getAnswers() 
    { 
     return $this->answers; 
    } 
} 

在Symfony2中,我想創建一個表單並作爲該表單的一部分顯示該實體的Options作爲「選項」。

我實現了以下內容:

$form = $this->createFormBuilder(); 
$form->add($question->getQuestionOrder(), 'choice',array('label' => $question->getQuestion(), 'choices' => $question->getOptions())); 
$formbuilder->add($question->getId(), 'checkbox',array('label' => $question->getQuestion(), 'choices' => $question->getOptions())); 

然而檢查的頁面我收到以下錯誤,是沒有意義的我,當:

The option "choices" does not exist. Known options are: "action", "attr", "auto_initialize", "block_name" ... 

怎麼來的Symfony2告訴我的選擇不存在因爲它顯然是表單設置的一部分?

實現此目的的正確方法是什麼?

+0

這看起來我的權利。你有其他類似的表單嗎? – frumious 2014-10-01 22:48:25

+0

我發現它,儘管它的名稱不使用多選擇表單類型的複選框。 您需要使用選項Multiple來使用選項。 – Gekko09 2014-10-02 08:57:33

回答

0

我發現了錯誤。

除了使用表單類型「複選框」,以渲染多個選擇的,你需要使用「選擇」與選項:

"multiple" => true, "expanded" => true 
+0

有點困惑,你的問題中的例子使用「選擇」類型,而不是「複選框」? – frumious 2014-10-02 14:14:14

+0

這就是我的錯,我沒有提交整個代碼(我將來會這樣做)。 我在複選框類型的選擇字段下方有另一個字段,並在其中添加了選項(認爲它會導致多個複選框)。 對不起 – Gekko09 2014-10-02 14:42:43

+0

啊哈!不用擔心,很高興你的工作。可能是編輯你的原始問題的一個想法,目前它與最終答案一起沒什麼意義,所以對於有類似問題的人來說這不會有任何幫助。 – frumious 2014-10-02 14:45:49

相關問題