简体   繁体   English

Symfony:嵌入式表单集合中的独特选项

[英]Symfony: unique options in embedded form collection

I have an embedded form collection in Symfony. 我在Symfony中有一个嵌入式表单集合。 Which is working nice. 哪个工作很好。 I am working with a manytomany assocation mapping. 我正在处理许多关联映射。 Except i want to create (with javascript?) the form so that only unique values are available. 除了我要创建(使用javascript吗?)表单,以便仅唯一值可用。 In my example i have an Organisation which can exist of many users. 在我的示例中,我有一个可以存在许多用户的组织。 When i add user "L" in this case and i want to add a second user i want to prevent that user "L" is available in the other dropdown. 当我在这种情况下添加用户“ L”并且要添加第二个用户时,我想防止该用户“ L”在其他下拉列表中可用。 The way i embedded the collection of forms is exactly like the documentation of Symfony has learned me. 我嵌入表单集合的方式就像Symfony的文档学到的一样。 ( http://symfony.com/doc/current/cookbook/form/form_collections.html ) http://symfony.com/doc/current/cookbook/form/form_collections.html

Down below is an example of how it works now (in this example I want to prevent that in the dropdown the user "L" is available as an option. Thank you very much! 下面是当前工作方式的示例(在此示例中,我要防止在下拉菜单中提供用户“ L”作为选项。非常感谢!

在此处输入图片说明

Unfortunately, there's no way you can make the HTML form behave this way. 不幸的是,您无法使HTML表单具有这种行为。 HTML forms just do not have any composite (or dependent) <select> widgets. HTML表单只是没有任何组合(或相关)的<select>小部件。 So, the right way to go here would be: 因此,正确的做法是:

  1. validating the form server-side, so that duplicate values in two select boxes are not allowed; 验证表单服务器端,以便不允许在两个选择框中出现重复的值;

  2. adding some JavaScript code to the view that renders form. 向呈现表单的视图中添加一些JavaScript代码。 Perhaps this code should listen to <select> s' onChange events, and once an event is received, the option selected in the first box should either receive a disabled attribute or get deleted. 也许此代码应侦听<select>onChange事件,并且一旦接收到事件,则在第一个框中选择的选项应该接收disabled属性或被删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM