Gekko09
Gekko09

Reputation: 51

Symfony2 Choices from Entity Field

I have an entity ChoiceQuestion which has a field Options. Options is defined as an Array.

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

In Symfony2 I want to create a form and as part of that form display the Options from that entity as "choices".

I implemented the following:

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

However when checking the page I get the following error which makes no sense to me:

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

How come Symfony2 tells me that choices does not exist since it is obviously part of the form setup?

What is the proper way to implement this?

Upvotes: 0

Views: 343

Answers (1)

Gekko09
Gekko09

Reputation: 51

I found the error.

Instead of using the form type "checkbox" to render multiple selects, you need to use "choice" with the options:

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

Upvotes: 0

Related Questions