瀏覽代碼

fixed choice field mask form type using immutable array form type (#4516)

Romain Mouillard 8 年之前
父節點
當前提交
0426521da4
共有 1 個文件被更改,包括 18 次插入14 次删除
  1. 18 14
      Resources/views/Form/form_admin_fields.html.twig

+ 18 - 14
Resources/views/Form/form_admin_fields.html.twig

@@ -463,37 +463,41 @@ file that was distributed with this source code.
 
 {% block sonata_type_choice_field_mask_widget %}
     {{ block('choice_widget') }}
-    {% set main_form_name = id|slice(0, id|length - name|length) %}
+    {# Taking the form name excluding ending field glue character #}
+    {% set main_form_name = id|slice(0, (id|length - name|length)-1) %}
     <script>
         jQuery(document).ready(function() {
             var allFields = {{ all_fields|json_encode|raw }};
             var map = {{ map|json_encode|raw }};
 
-            var showMaskChoiceEl = jQuery('#{{ main_form_name }}{{ name }}');
+            var showMaskChoiceEl = jQuery('#{{ main_form_name }}_{{ name }}');
 
             showMaskChoiceEl.on('change', function () {
                 choice_field_mask_show(jQuery(this).val());
             });
 
-            function choice_field_mask_show(val)
-            {
+            function choice_field_mask_show(val) {
                 var controlGroupIdFunc = function (field) {
-                    return '#sonata-ba-field-container-{{ main_form_name }}' + field;
+                    // Most of fields are named with an underscore
+                    var defaultFieldId = '#sonata-ba-field-container-{{ main_form_name }}_' + field;
 
+                    // Some fields may be named with a dash (like keys of immutable array form type)
+                    if (jQuery(defaultFieldId).length === 0) {
+                        return '#sonata-ba-field-container-{{ main_form_name }}-' + field;
+                    }
+
+                    return defaultFieldId;
                 };
-                if (map[val] == undefined) {
-                    jQuery.each(allFields, function (i, field) {
-                        jQuery(controlGroupIdFunc(field)).hide();
-                    });
-                    return;
-                }
 
                 jQuery.each(allFields, function (i, field) {
                     jQuery(controlGroupIdFunc(field)).hide();
                 });
-                jQuery.each(map[val], function (i, field) {
-                    jQuery(controlGroupIdFunc(field)).show();
-                });
+
+                if (map[val]) {
+                    jQuery.each(map[val], function (i, field) {
+                        jQuery(controlGroupIdFunc(field)).show();
+                    });
+                }
             }
             choice_field_mask_show(showMaskChoiceEl.val());
         });