Symfony2 Forms - how to use dynamic selection options

I am working on a form that is used to send notifications. In the form, I have, among other fields, one input file field. In this field I upload a CSV file. From CSV (contains a column of email messages) I extract the data using https://code.google.com/p/jquery-csv/ and add it to the select readonly multiple field with all the options selected.

Note. I created a form using form forms (from Symfony2 book) - class NotificationType extends AbstractType ; The form is submitted via AJAX;

1. Beat one Considering that the field of choice is filled dynamically, I decided not to add it to the form designer, but to enter it using JavaScript.

This was a bad move, because when submitting a form, Symfony2 confirms that the fields added to the form builder are the exact ones that are submitted. Return error: this form should not contain additional fields.

2. Hit two. Moving from the strike, I wanted to see if I could get around this, so I added a selection box in the form builder, but without parameters, and also entered the options using JavaScript.

Still out of luck. Symfony2 also ensures that you cannot submit a parameter that was not added to the form designer when adding the field. Return error: this value is invalid.

3. Hit three From Strike 1 and 2, I assume that the only way to do this work is to add all the possible options in the form builder to the selection array when adding a field. Thus, the field and parameters will be valid for sending.

But this is not what I consider a viable option, especially because in the selection box I expect 10,000 possible results, which I don’t know about until I upload the CSV file.

Can any other approach be suggested for this?

Thanks!

+5
source share
1 answer

See how to dynamically change forms (example number 3).

You will need to do this, since the form will be checked based on the linker’s parameters, therefore, if you add a new field or change its parameters during visualization, it will not know about it.

Or you do not map the form to the model / entity and process it yourself.

+4
source

All Articles