具有多个依赖字段的 Symfony2 表单 [英] Symfony2 forms with multiple dependent fields

查看:28
本文介绍了具有多个依赖字段的 Symfony2 表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个具有以下格式的注册.

I am creating a registration from which have the following format.

如果国家发生变化,那么我需要相应地更改子字段.

If country changes then i need to change the child fields accordingly.

1- 国家状态城市郊区

1- Country States Cities Suburbs

在我的 COMPANY BRANCH 实体中,我将郊区密钥保存为一对多.公司分公司位于哪个郊区.

In my COMPANY BRANCH entity i am saving suburb key as one-many. Company branch located in which suburb.

如果我改变了公司集团,那么它应该改变孩子的.

if i change the company group then it should change the child's.

2- 公司集团公司

在我的 COMPANY BRANCH 实体中,我将公司密钥保存为外键.一对多的关系.公司有多个分公司,一个分公司只属于一个公司.

In my COMPANY BRANCH entity i am saving company key as foreign key. One- many relationship. Company has many branches and one branch only belong to one company.

3- 分行地址(文本)

3- Branch address (text)

4- 一些其他文本字段.

4- some other text fields.

问题:如何通过嵌入式表单、集合或简单的 Ajax 调用来实现这一点.

QUESTION: How to achieve this either Embedded forms, collections or simple Ajax calls.

我试过从属形式教程,但我有两个依赖关系.国家和公司组.对我来说,很难在我的项目中映射这些东西.如果有人提供详细说明如何操作.那很好啊.谢谢,

i have tried Dependent forms tutorial, but i have two dependencies. Country and Company group. For me its difficult to map the stuff in my project. If someone provide detail description how to do it. That would be great. Thanks,

推荐答案

在我看来,实现这一目标的最佳方法是将您的子字段设置为禁用",为每个字段设置一个空值,并附加每个父下拉列表的 jQuery 侦听器.

In my opinion the best way to achieve this is to set your child fields as "disabled", to set an empty value to each of them, and to attach a jQuery listener to each of your parent dropdown lists.

这样,您将能够通过对控制器的 ajax 调用获取子字段的值,该控制器会向您发送回 JSON 数组.

That way, you'll be able to get the values for your child fields with an ajax call to a controller which would send you back a JSON array.

例如,您的第一个父/子元素是Country"和States":

For example, your first parent/child element is "Country" and "States" :

<select name="country_list">
    <option value="1">USA</option>
</select>

<select name="states_list">
    <option value="0">-- empty value --</option>
</select>

那么你的 jQuery 应该是这样的:

Then your jQuery should be something like this :

$("#country_list").change(function() {
    var countryId = $('#country_list').val();
    var callUrl = 'CONTROLLER URL TO CALL';

    // make your ajax call here
    $.ajax({
       url : callUrl,
       type : 'GET',
       dataType : 'json',
       success : function(response){
                     // populate the child list with the JSON values
                     $.each(response,function(key, value) {
                         $('#states_list').append('<option value=' + key + '>' + value + '</option>');
                     });

                     // enable the child list 
                     $('#states_list').prop('disabled', false);
                 }
    });
});

当然你的控制器应该返回一个 JSON 数组.

Of course your controller should return a JSON array.

这篇关于具有多个依赖字段的 Symfony2 表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆