如何在表单Symfony2中添加自动填充字段?

时间:2022-10-16 18:20:24

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.

提前致谢。

1 个解决方案

#1


20  

Two things you need to do:

你需要做的两件事:

1) Create a Data Transformer

1)创建数据变换器

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

hich将允许您使用输入字段而不是链接实体的默认下拉列表。

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

2)创建Ajax自动完成

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.

这可以使用您想要的任何库(jQuery,Prototype等)在客户端完成,服务器端可以在Controller操作中处理它并使用JSON或呈现的模板进行回复。这里有很多教程。如果没有找到,Ajax结果可能包含创建新用户的链接。

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

数据转换器是您需要关注的,我会在开始使用UI之前使用它。

#1


20  

Two things you need to do:

你需要做的两件事:

1) Create a Data Transformer

1)创建数据变换器

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

hich将允许您使用输入字段而不是链接实体的默认下拉列表。

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

2)创建Ajax自动完成

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.

这可以使用您想要的任何库(jQuery,Prototype等)在客户端完成,服务器端可以在Controller操作中处理它并使用JSON或呈现的模板进行回复。这里有很多教程。如果没有找到,Ajax结果可能包含创建新用户的链接。

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

数据转换器是您需要关注的,我会在开始使用UI之前使用它。