Symfony2 Sonata管理员根据所选值动态更改输入数据 [英] Symfony2 Sonata admin dynamically change input data based on selected value

查看:115
本文介绍了Symfony2 Sonata管理员根据所选值动态更改输入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的对象之一,我需要创建一些动态的表单呈现...但是我无法弄清楚如何在Sonata Admin中执行此操作.例如,当我创建一个对象时,我具有一个字段类型.在此字段中,选择一种对象将要成为的类型.现在,当我选择类型时,我想根据该类型显示一个字段.例如,如果我选择类型"Carousel",我想显示一个选择所有对象形式的实体图库的字段.如果我选择类型产品",我想显示所有要选择的产品的字段...我该如何实现?

For one of my objects I need to create some dynamic form rendering... But I cant figure out how to do this in Sonata Admin. For example when I create an object I have a field type. In this field I select a type that my object is going to be. Now when I select the type I want to make a field appear, based on the type. For example, if I select type "Carousel" I want to show a field that is selecting all object form entity Gallery. If I select type "Product" I want to display field with all products to selectt from... How can I acheve that?

现在我有这个了

/**
 * @param FormMapper $formMapper
 */
protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->with('Module', array(
            'class' => 'col-md-6'
        ))
            ->add('position')
            ->add('type', null, array(
                'attr' => array('class' => 'module_type')
            ))
            ->add('items', 'entity', array(
                'class' => 'ApplicationSonataMediaBundle:Gallery'
            ))
        ->end()
    ;
}

并且我已经覆盖了编辑模板:

And I have overriden the edit template:

{% extends 'SonataAdminBundle:CRUD:edit.html.twig' %}

{% block javascripts %}
    {{ parent() }}
    <script type="text/javascript">
        $(document).ready(function () {
            $(".module_type").change(function() {

            });

        });
    </script>

{% endblock %}

如您所见,图库现在已被硬编码.

As you can see the gallery is hardcoded now..

我现在不知道该怎么做...怎么说,如果选择的值是这个,请在​​字段中使用该实体...问题是,在Sonata中渲染表单的方式非常复杂.我不明白.

I cant figure out how to do this now... How to say that if the value selected is this, use that entity in field... The problem is that the way form is rendered in Sonata is very complicated... I dont understand it..

也许我应该使用ajax?但是再次,当我发送一个值并获得响应时,如何在不刷新的情况下添加字段?

maybe I should use ajax? But again, when I send a value and get the response how to add a field without refresh?

任何帮助表示赞赏.

推荐答案

Sonata为您提供了"sonata_type_choice_field_mask"类型,该类型使您可以根据此"sonata_type_choice_field_mask"输入值来动态更改表单上显示的字段,以便您不必使用ajax.

Sonata provides you with the 'sonata_type_choice_field_mask' type which allows you to change the fields displayed on the form dynamically depending on the value of this 'sonata_type_choice_field_mask' input so you don't have to use ajax.

此处是文档,您可以在其中找到有关奏鸣曲类型和选择字段掩码的所有信息.

Here is the doc where you can find everything about sonata types and the choice field mask.

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('type', 'sonata_type_choice_field_mask', array(
            'choices' => array(
                //The list of available 'Type' here
                'choice1',
                'choice2'
            ),
            'map' => array(
                //What you want to display depending of the selected option
                'choice1' => array(
                    // List of the fields displayed if choice 1 is selected
                    'field1', 'field3'
                ),
                'choice2' => array(
                    // List of the fields displayed if choice 2 is selected
                    'field2', 'field3'
                )
            ),
            'placeholder' => 'Choose an option',
            'required' => true
        ))
        ->add('field1', 'entity', array(/* Options for entity1 goes here */))
        ->add('field2', 'entity', array(/* Options for entity2 goes here */))
        ->add('field3')
    ;
}

这篇关于Symfony2 Sonata管理员根据所选值动态更改输入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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