2014-10-01 141 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

相关问题