【问题标题】:symfony form label attribute 'for' not changing according to form_widget 'id' change while passing through templatesymfony 表单标签属性 'for' 不会根据 form_widget 'id' 更改而通过模板更改
【发布时间】:2017-01-15 11:11:26
【问题描述】:

我正在渲染一个包含 symfony 中的表单域的表单 在我的模板中,我正在逐部分渲染表单 像这样 {{- form_label(form.category,'Categoryname',{ 'label_attr' : {'class': 'control-label','for':'Cname'}}) -}} {{- form_widget(form.category,{ 'attr': {'class': 'form-control col-md-10','max-length':55},'label': 'Category name','required':true,'id':'Cname' }) -}}

但是表单字段的 'id' 更改为 'Cname' 但标签中的 for 属性没有改变它显示 symfony 的默认值 如何更改表单字段的“id”和相应标签的“for”属性?

【问题讨论】:

    标签: forms symfony twig


    【解决方案1】:

    您可以自定义 form_label。详情请见Making Application-wide Customization。存储在https://github.com/symfony/symfony/blob/2.7/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig 中的原始表单模板。例如,您可以更改块 form_label

    的部分
    {% if not compound -%}
        {% set label_attr = label_attr|merge({'for': id}) %}
    {%- endif -%}
    

    {% if not compound -%}
        {% set label_attr = {'for': id}|merge(label_attr) %}
    {%- endif -%}
    

    现在 label_attr 中的自定义参数 for 将覆盖默认小部件 id

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 2014-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-18
      • 1970-01-01
      相关资源
      最近更新 更多