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 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算