简体   繁体   English

Symfony2如何更改表单ID

[英]Symfony2 How To Change The Form ID

I have a form field that I want to change the ID on when rendering in Twig. 我有一个表单字段,我想在Twig中渲染时更改ID。 For some reason it is not adding the ID to the form field. 由于某种原因,它没有将ID添加到表单字段中。

Here is how I am doing this in Twig: 这是我在Twig中这样做的方式:

 {{ form_widget(form.purchaseOrderLineItem, { 'value': value, 'id': 'new_id',  'attr': {'style': 'display:none'} }) }} 

Here is what it is outputting: 这是输出的内容:

  <input class="form-control" type="text" value="203543" />

Why is it not creating the form field with the ID? 为什么不使用ID创建表单字段?

There is no sense to add an id attribute, because he hardcode in the template form_div_layout.html.twig . 添加id属性是没有意义的,因为他在模板form_div_layout.html.twig中进行了硬编码。 But you can override it like this: 但是您可以像这样覆盖它:

{% form_theme form _self %}

{% block widget_attributes %}
    {% spaceless %}
        {% if attr.id is not defined %}id="{{ id }}"{% endif %} name="{{ full_name }}"{% if read_only %} readonly="readonly"{% endif %}{% if disabled %} disabled="disabled"{% endif %}{% if required %} required="required"{% endif %}{% if max_length %} maxlength="{{ max_length }}"{% endif %}{% if pattern %} pattern="{{ pattern }}"{% endif %}
        {% for attrname, attrvalue in attr %}{% if attrname in ['placeholder', 'title'] %}{{ attrname }}="{{ attrvalue|trans({}, translation_domain) }}" {% else %}{{ attrname }}="{{ attrvalue }}" {% endif %}{% endfor %}
    {% endspaceless %}
{% endblock widget_attributes %}

{% block body %}
    {{ form(form) }}
{% endblock %}

id必须位于attr部分:

{{ form_widget(form.purchaseOrderLineItem, { 'value': value,  'attr': {'style': 'display:none', 'id':'new_id'} }) }} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM