dongxi9326 2016-12-26 07:18
浏览 49
已采纳

Symfony 3无线电输入名称更改

I have a form which has a form (CustomerVatsType) for an entity (CustomerVats). This entity has a column (vats), which contains multiple vat rows. These rows are saved in json format. On this form, customer can choose a default vat, which will be saved in "default" index of rows saved in units column.

but problem is that radio input name is "form[vats][0][set_default]" due to structure of form. But for radio input to work correctly it needs to be same for all inputs (e.g. form[vats][set_default]). I can change name in twig file but then form class can not understand this.

What can be done for this situation. Does even Symfony support it. Here is my form class.

class VatsType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
        ->add('vats', CollectionType::class, array(
                'entry_type'   => VatType::class,
                'allow_add'    => false,
                'allow_delete' => false,
                'prototype'    => false,
                'by_reference' => false,
            )
        )
        ->add('vatSumbit', SubmitType::class);
    }
}


class VatType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
        ->add('country_id', HiddenType::class, array('label' => false))
        ->add('vat_high', HiddenType::class, array('label' => false))
        ->add('vat_low', HiddenType::class, array('label' => false))
        ->add('vat_zero', HiddenType::class, array('label' => false))
        ->add('vat_none', HiddenType::class, array('label' => false))
        ->add('set_default', RadioType::class, array('label' => false))
        ->add('set_show', RadioType::class, array('label' => false));
    }
}
  • 写回答

1条回答 默认 最新

  • dounao2829 2016-12-26 12:41
    关注

    I ended up using following fix for this problem. It seems bit dirty but made most sense to because I dont want to confuse other devs with some really difficult method.

    <script type="text/javascript">
        (function (document, window, $) {
            $('[data-radio-field]').change(function () {
                var field = $(this).data('radio-field');
                $('[data-radio-field="' + field + '"]').not($(this)).prop('checked', false);
            });
        })(document, window, jQuery);
    </script>
    

    If anyone can suggest a clean method, they are welcome to answer it. I think it should be a common problem. Would like to see other approaches well.

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

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码