add( 'membership', 'entity', -6ren">
gpt4 book ai didi

html - 交响乐 2 : Form entity field add "data-*" attribute on each radio input

转载 作者:行者123 更新时间:2023-12-02 04:11:43 26 4
gpt4 key购买 nike

我有一个表单,其中包含一个显示单选字段的“实体”类型的字段:

->add(
'membership',
'entity',
array(
'class' => 'Comiti\UserBundle\Entity\Membership',
'expanded' => true,
'multiple' => false,
'label' => false,
'empty_value' => 'Aucune adhésion',
'query_builder' => function (MembershipRepository $er) {
return $er->createQueryBuilder('membership')
->where('membership.club = :club')
->setParameter('club', $this->authentication_service->getCurrentClub())
->orderBy('membership.name', 'ASC')
;
},
)
)

我正在像这样在我的 View 中添加单选字段:

{{ form_row(form.membership) }}

在 html 中呈现类似的内容:

<input type= "radio" name="membership" value="1">
<input type= "radio" name="membership" value="2">
<input type= "radio" name="membership" value="3">

我想为每个输入添加“数据价格”:

<input type= "radio" name="membership" data-price="20" value="1">
<input type= "radio" name="membership" data-price="10" value="2">
<input type= "radio" name="membership" data-price="15" value="3">

我该怎么做?

最佳答案

如果您使用的是 symfony >= 2.7,则可以使用“choice_attr”

http://symfony.com/doc/current/reference/forms/types/entity.html#choice-attr

$builder->add('attending', 'choice', array(
'choices' => array(
'Yes' => true,
'No' => false,
'Maybe' => null,
),
'choices_as_values' => true,
'choice_attr' => function($val, $key, $index) {
// adds a class like attending_yes, attending_no, etc
return ['class' => 'attending_'.strtolower($key)];
},
));

关于html - 交响乐 2 : Form entity field add "data-*" attribute on each radio input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36313658/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com