Noz
Noz

Reputation: 85

Multiple choice from database without <select>

I'm trying to make a form with Symfony to be able to select multiple choice by checking checkboxes without having a <select> dropdown alone in my form.

Theses choice come from a table in my database, each row must be a choice possibility in my form.

For example table T_Choices :

ID Choices
1 Choice_1
2 Choice_2

I'm able to create the form with many checkbox as there are rows.

But how can i submit this "dynamic" form and get thoses data in one "array" in my controller to have for example $form["choices"]->getData()[0] or 1 or 2 etc.

Thank you for your help. And sorry for my bad english.

Upvotes: 1

Views: 85

Answers (1)

Siebe Jongebloed
Siebe Jongebloed

Reputation: 4844

By giving those checkboxes the same value for the name-attribute and adding square brackets at the ending []. You probably need:

<input id="1" name="choices[]" type="checkbox" value="1"><label for="1">Choice_1</label>
<input id="2" name="choices[]" type="checkbox" value="2"><label for="2">Choice_2</label>

Upvotes: 1

Related Questions