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.

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

报告相同问题?

悬赏问题

  • ¥20 ue5运行的通道视频都会有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数