【问题标题】:Symfony2 Choices from Entity FieldSymfony2 实体字段的选择
【发布时间】:2014-10-01 22:14:10
【问题描述】:

我有一个实体 ChoiceQuestion,它有一个字段 Options。 选项被定义为一个数组。

<?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 中,我想创建一个表单,并作为该表单的一部分将来自该实体的选项显示为“选择”。

我实现了以下内容:

$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 告诉我选择不存在,因为它显然是表单设置的一部分?

实现这一点的正确方法是什么?

【问题讨论】:

  • 这对我来说是正确的。你还有其他类似的表格吗?
  • 我找到了它,尽管它的名字你不使用类型复选框作为多选表单类型。您需要将选项与选项 Multiple 一起使用。

标签: symfony


【解决方案1】:

我发现了错误。

您需要使用带有选项的“选择”,而不是使用表单类型“复选框”来呈现多个选择:

"multiple" => true, "expanded" => true

【讨论】:

  • 对此有点困惑,您问题中的示例使用choice 类型,而不是checkbox?
  • 那是我的错,我没有提交整个代码(我以后会这样做)。我在复选框类型的选择字段下方有另一个字段,并在那里添加了选择(认为它会导致多个复选框)。很抱歉造成混乱
  • 啊哈!不用担心,很高兴你让它工作。编辑原始问题可能是一个想法,目前它与最终答案没有多大意义,因此对遇到类似问题的人没有任何帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 2012-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多