Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an autocomplete field in forms Symfony2?

Tags:

forms

symfony

Actually, I can assign a task to a user in the edition task. I display a dropdown list of all user in the system when I edit a task. Now, I would to be able to display a text input with autocomplete for user, and be able to add user if is not exist.

How to do that?

Thanks in advance.

like image 436
gperriard Avatar asked Jun 05 '12 09:06

gperriard


1 Answers

Two things you need to do:

1) Create a Data Transformer

hich will let you use an input field instead of the default dropdown to the linked entity.

The Data Transformer basically transforms your input (in your case a username string) to actual user object that you can store with your entity. It will let you define an input field instead of the default dropdown to the linked entity. Have a look at the documentation, it's pretty straight forward.

2) Create the Ajax autocomplete

This can be done using any library you want (jQuery, Prototype etc.) on the client side, and server side you handle this in your Controller action and replies with either JSON or a rendered template. There are plenty of tutorials for this out there. The Ajax result could include a link to create a new user, if none is found.

The Data Transformer is what you need to focus on, and I would get that working before working on the UI.

like image 186
cvaldemar Avatar answered Oct 09 '22 19:10

cvaldemar