Symfony2自定义radio_widget实体访问

时间:2016-03-31 09:09:16

标签: php forms symfony

我有一个包含3个显示无线电输入的实体字段的表单。

->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')
                    ;
                }, 
            )
        )->add(
            'federal_license',
            'entity',
              array(
                'class' => 'Comiti\UserBundle\Entity\FederalLicense',
                'expanded' => true,
                'multiple' => false,
                'label' => false,
                'empty_value' => 'Aucune licence',
                'query_builder' => function (FederalLicenseRepository $er) {
                    return $er->createQueryBuilder('federal_license')
                        ->where('federal_license.club = :club')
                        ->setParameter('club', $this->authentication_service->getCurrentClub())
                        ->orderBy('federal_license.name', 'ASC')
                    ;
                }
            )
        )->add(
            'insurance',
            'entity',
              array(
                'class' => 'Comiti\UserBundle\Entity\Insurance',
                'expanded' => true,
                'multiple' => false,
                'label' => false,
                'empty_value' => 'Aucune assurance',
                'query_builder' => function (InsuranceRepository $er) {
                    return $er->createQueryBuilder('insurance')
                        ->where('insurance.club = :club')
                        ->setParameter('club', $this->authentication_service->getCurrentClub())
                        ->orderBy('insurance.name', 'ASC')
                    ;
                }
            )
        );

我需要为那些无线电输入定义一个自定义模板,在每个输入中输入一个“数据价格”的attr。 我这样做了:

{%- block radio_widget -%}
<input type="radio" data-price="{{Myprivcevar}}" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />

是否有可能访问{{MypriceVar}} 我在Symfony 2.6

1 个答案:

答案 0 :(得分:0)

您可以在不创建自定义模板的情况下向输入添加属性:

{{ form_widget(yourRow, {'attr': {'data-price':'yourValue'}}) }}

示例:

{{ form_widget(choiceFormView, {'attr': {'data-price':'2'}}) }}

结果

<input type="radio" id="form_choice_0" name="form[choice]" required="required" data-price="2" value="1" checked="checked">