dsi37923 2015-12-15 13:16
浏览 54
已采纳

具有EntityType字段的Symfony 2.7 choice_attr

I need to add an additional HTML attribute to each choice of an EntityType field in Symfony 2.7.

Following this guide, I assume that EntityType inherits this feature from ChoiceType. I tried something like the following, but without effect; no mytype attribute gets added to the rendered select options.

$builder->add('customer_email', 'email')
        ->add('Product', 'entity', array(
              'class' => 'MyBundle:Product',
              'property' => 'name',
              'empty_value' => 'None',
              'required' => false,
              'choice_attr' => function ($val, $key, $index) {
                  return array('mytype' => $val->getType());
              }))
  • 写回答

1条回答 默认 最新

  • dongqiang2358 2016-02-18 22:11
    关注

    This is not necessarily the best answer, but I can't post comments yet.

    When implementing choice_attr, choice_labels etc... on ChoiceType and EntityType, it seems like choice_attr was left behind on the latter, there are a few comments about it on github, I personally need the same feature, hopefully it'll be implemented.

    https://github.com/symfony/symfony/issues/4067

    P.S.: Investigated further, it is indeed inherited from ChoiceType, and it only appears in the 2.7 documentation, if you write something like

    'choice_attr' => function (Product $product, $key, $index) {
        return ['class' => $product->getType() ];
    }
    

    You should get the class attribute set correctly, for custom attributes I am not sure, you might need to use 'attr' => 'foo'.

    P.P.S.: Tested 'foo' =>'bar' and it works, no need to nest inside 'attr'.

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

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: