Symfony form builder with radio buttons

I am using Symfony framework, and I need to create a form that will have some text fields, a submit buton and four radio buttons. Each radio button will represent a field in my database (answer1, answer2, answer3, answer4) for a quiz with four possible answers. So far have created the text field and not sure how I can create the four radio buttons.
Can someone help? Thanks

This is what I have so far for the text fields and the submit button.

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
    ->add('id',HiddenType::class)
    ->add('image',TextType::class)
    ->add('question',TextType::class)

    ->add('Submit',SubmitType::class);
}

I have read and read the documentation over and over. I can create the text fields but I can’t get my head arround how to create the radio buttons. In symfony I have an entity which is mapped to a database table. The entity has thees properties: id, image, question, answer1, answer2, answer3, answer4. Therefore I am trying to build a form that will display a question with 4 anwers, where the user can choose one.

are you aware how to create radio buttons in principal (I posted the link for that)? then you populate the choices option with the values from the DB.

public function buildForm(FormBuilderInterface $builder, array $options)
    {
    $builder
        ->add('id',HiddenType::class)
        ->add('image',TextType::class)
        ->add('question',TextType::class)
        ->add('answer1',ChoiceType::class,
            array('choices' => array(
                    'answer1' => '1',
                    'answer2' => '2',
                    'answer3' => '3',
                    'answer4' => '4'),
            'choices_as_values' => true,'multiple'=>false,'expanded'=>true))


    ->add('Submit',SubmitType::class);
    }

This is my code to create the radio buttons, and this is the screenshot:

The text fields have inside them the data from the DB, but the radio buttons don’t display the data from the database. the fields in the database are: id,image,question,answer1,answer2,answer3,answer4. Also `->add('answer1, I need to put answer1 as a property the exists in my entity.If i put something else, then it will say it did not find any methods in my entity class.

because you defined them manually (the choices option). additionally, a single-choice field (dropdown, radio buttons) maps to a single value, not four. this is a drawback of your chosen DB design.

Given your DB design, the form would need to look like this to make sense for Symfony:

$builder
    ->add('id',HiddenType::class)
    ->add('image',TextType::class)
    ->add('question',TextType::class)
    ->add('answer1',TextType::class,
    ->add('answer2',TextType::class,
    ->add('answer3',TextType::class,
    ->add('answer4',TextType::class,
    ->add('Submit',SubmitType::class);

Yes, you are right. How do I need to modify my database design? At the moment there is only one quiz question in my DB so I can still do that.

assuming you use MySQL I’d use something along:

CREATE TABLE answers (
    id     INT NOT NULL PRIMARY KEY,
    answer TEXT NOT NULL
)

CREATE TABLE questions (
    id       INT NOT NULL PRIMARY KEY,
    image    VARCHAR(127) NOT NULL,
    question TEXT NOT NULL
)

CREATE TABLE choices (
    question  INT NOT NULL,
    answer    INT NOT NULL,
    isCorrect TINYINT(1) NOT NULL DEFAULT FALSE,
    PRIMARY KEY (question, answer),
    FOREIGN KEY answer   REFERENCES answers (id),
    FOREIGN KEY question REFERENCES questions (id)
)

Thanks. I will create thees three tables and their associations.

Just curious, why are you not simply using Doctrine annotations? You could easily create a new Choices entity with a one-to-many relation to as many answers as you like, and have everything mapped automatically…

I will create the entity Choices using the command line and doctrine, but I am not sure how I can create the one to many relation from questions entity to the choices entity.

Ah okay. You’d have to modify your Answer entity like that (roughly):

class Answer {
   // Answer id etc...

   /**
     * @var Choices
     *
     * @ORM\ManyToOne(targetEntity="Choices")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="choices", referencedColumnName="id_choices")
     * })
     */
    private $choices;

    // etc...
}

… where id_choices has to be replaced with the actual name of the Choices ID column. Then have doctrine recreate your database.

Edit: Oops, that’s just many-to-one, obviously. ^^ If you want a one-to-many relation as well, that’s possible too though. Just check the handbook! :-)

Thanks. I will give it a try a bit later on. Just starting to learn symfony now :slight_smile:

I have my questions entity and my answers entity. Here is my mapping:

class Questions
{
    /**
     * @var ArrayCollection;
     *
     * @ORM\OneToMany(targetEntity="Answers", mappedBy="questions")
     *
     */
    private $answers;

public function __construct()
    {
        $this->answers = new ArrayCollection();
    }

,

class Answers
{
    /**
     * @ORM\ManyToOne(targetEntity="Questions", inversedBy="answers")
     * @ORM\JoinColumn(name="choices_id", referencedColumnName="id")
     */
    private $questions;

Is my mapping correct?

the whole DB setup looks wrong to me (it’s essentially the same as before, only spread over two tables), but I’m no expert on Doctrine ORM.

Maybe one to one is better. each question is mapped in the other table to one row with all the answers:

class Questions
{

    /**
     * @ORM\OneToOne(targetEntity="Answers", mappedBy="questions")
     */
    private $answers;


class Answers
{

    /**
     * @ORM\OneToOne(targetEntity="Questions", inversedBy="answers")
     * @ORM\JoinColumn(name="question_id", referencedColumnName="id")
     */
    private $questions;

this layout created the problems you encountered in the first place …

Well at this point I really don’t understand what I should do to make it work with the radio buttons…
The design makes more sense to have the answers in a different table and when displaying a question call all the answers from the answers table.

IMO the primary problem is that you want the form to do something completely different (let a user choose an answer for a question) than what is does now (edit the questions/answers).

the secondary problem is that the answers should not be stored in multiple columns (because they all have the same purpose). your current design is a violation of the 1NF (first normal form).