How can be implemented HTML5 datalist with values from the database (Doctrine)?
Purpose: replace selects with many options to inputs with autocompletion.
How can be implemented HTML5 datalist with values from the database (Doctrine)?
Purpose: replace selects with many options to inputs with autocompletion.
In your formType :
In your View :
I spent some time trying to solve this issue, and there is a quite simple solution which solves Konstantin's issue with database access. It is to create a new form type which has EntityType as it's parent.
You can then create a new template for this widget:
Finally, in the buildForm function of the form you are building, you can add your form element using the DatalistType, and using the EntityType options.
First, add your new
FormType
for the field:.In
services.yml
:Do you have a form theme? If yes, skip to the next step, if no, create a new one in
app/Resources/views/Form/fields.html.twig
and change your default Twig theme to it:Now define a template for your new field in the form theme:
Use your field in
FormType
:Instead of
['a', 'b']
You need to load your choices from DB somehow, I'd suggest passing them in form options as the easiest solution.