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 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死