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.
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.
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)
)
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.
… 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! :-)
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).