drh19790711 2016-12-12 19:47
浏览 27
已采纳

Symfony 2 - 根据另一个字段中的选择动态隐藏字段

I've searched a lot, but can't find the answer to this rather simple question:

In Symfony 2.8 I have a form, that renders a country field. Based on it's selection I want to display a zipcode and federalstate field only for the choice 'Deutschland' (data 'D'). Here is the code for the country-field:

                ->add('nationveranstaltungsort', ChoiceType::class, array(
                    'choices' => array(
                        'D' => 'Deutschland',
                        'AFG' => 'Afghanistan',
//many other countries following                   
                    ),
                    'data' => 'D',
                    'label' => 'Nation Tagungsstätte'))

it's prepopulated with the Choice 'Deutschland', data 'D'.

based on that i want to dynamically display:

->add('plzveranstaltungsort', TextType::class, array('label' => 'PLZ', 'attr' => array('size' => '5', 'maxlength' => '5')))
                ->add('bundeslandveranstaltungsort', ChoiceType::class, array(
                    'choices' => array(
                        0 => '     ',
                        1 => 'Baden-Württemberg',
                        2 => 'Bayern',
                        3 => 'Berlin',
                        4 => 'Brandenburg',
                        5 => 'Bremen',
                        6 => 'Hamburg',
                        7 => 'Hessen',
                        8 => 'Mecklenburg-Vorpommern',
                        10 => 'Nordrhein-Westfalen',
                        11 => 'Rheinland-Pfalz',
                        12 => 'Saarland',
                        13 => 'Sachsen',
                        14 => 'Sachsen-Anhalt',
                        15 => 'Schleswig-Holstein',
                        16 => 'Thüringen',
                    ),
                    'data' => 0,
                    'label' => 'Bundesland Tagungsstätte'))

If "Deutschland" is not selected, I simply want to hide/disable these fields (and handle the null value somewhere else).

I tried http://symfony.com/doc/current/form/dynamic_form_modification.html and some other javascript stuff, but as I basically don't know any JS, I couldn't find any proper resources.

  • 写回答

1条回答 默认 最新

  • duanqiangwu9332 2016-12-12 21:31
    关注

    Since by default the nationveranstaltungsort field has Germany selected, then there is no need to hide plzveranstaltungsort. You can hide in Javascript by using something like:

    var bund = document.getElementById("bundeslandveranstaltungsort");
    bund.style.display = "none";
    

    Then to show the element use:

    bund.style.display = "inline";
    

    Depending on what css you are using, you might need to use:

    bund.style.display = "block";

    In you form you can specify an attribute to run a Javascript function on change like so:

    ->add('nationveranstaltungsort', ChoiceType::class, array(
        'choices' => array(
            'D' => 'Deutschland',
            'AFG' => 'Afghanistan',
        ),
        'data' => 'D',
        'label' => 'Nation Tagungsstätte',
        'attr' => array(
                'onchange' => 'changeCountry()',
        ),
    ))
    

    Then in the Javascript function changeCountry() hide the element or show it using the above. The ids I show are not actually what you will use, but if you use the browser tools, for example in Firefox (I suggest using FireFox for testing), you can right-click on the choice element and choose "Inspect element"; then you can see what the "actual" id value is that you need to use. It depends on your Form name.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条