gpt4 book ai didi

symfony - 如何获取输入表格类型

转载 作者:行者123 更新时间:2023-12-03 13:42:39 25 4
gpt4 key购买 nike

我想要获取表单字段类型并为该字段类型设置类
我尝试:

{# Form field row #}
{% block form_row %}
{% spaceless %}
<div class="field-group{% if errors|length > 0%} error{%endif%}" id="fc-{{ id }}">
{{ form_label(form, label|default(null)) }}
<div class="field-item {{ type }}">
{{ form_widget(form) }}
{% if errors|length > 0 %}
<div class="errors">{{ form_errors(form) }}</div>
{% endif %}
</div>
</div>
{% endspaceless %}
{% endblock %}

但是{{type}}无法正常工作。

最佳答案

您可以使用以下方法获取字段类型:

{{ form.FIELD_NAME.vars.block_prefixes.2 }}


因此,如果表单中有一个名为message的字段,请使用以下命令:

{{ form.message.vars.block_prefixes.2 }}


对于嵌套表单字段类型,请使用以下命令:

{{ form.NESTED_FORM_NAME.FIELD_NAME.vars.block_prefixes.2 }}


编辑:

要覆盖基本表单块,请在模板文件中执行以下操作:

....
{% form_theme form _self %}
{% block widget_attributes %}
{% spaceless %}
id="{{ id }}" 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 %}
{% if not attr.class is defined %}
class="{{ type|default('text') }}"
{% endif %}
{% for attrname, attrvalue in attr %}{% if attrname in ['placeholder', 'title'] %}{{ attrname }}="{{ attrvalue|trans({}, translation_domain) }}" {% elseif attrname == 'class' %}{{ attrname }}="{{ type|default('text') }} {{ attrvalue }}"{% else %}{{ attrname }}="{{ attrvalue }}" {% endif %}{% endfor %}
{% endspaceless %}
{% endblock widget_attributes %}
{% block content %}
....
{% endblock %}


或获取更好的类型:

....
{% form_theme form _self %}
{% block widget_attributes %}
{% spaceless %}
id="{{ id }}" 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 %}
{% if not attr.class is defined %}
class="{{ form.vars.block_prefixes.2 }}"
{% endif %}
{% for attrname, attrvalue in attr %}{% if attrname in ['placeholder', 'title'] %}{{ attrname }}="{{ attrvalue|trans({}, translation_domain) }}" {% elseif attrname == 'class' %}{{ attrname }}="{{ form.vars.block_prefixes.2 }} {{ attrvalue }}"{% else %}{{ attrname }}="{{ attrvalue }}" {% endif %}{% endfor %}
{% endspaceless %}
{% endblock widget_attributes %}
{% block content %}
....
{% endblock %}

关于symfony - 如何获取输入表格类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13196689/

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