gpt4 book ai didi

php - Symfony2 中的 HTML 表单标签而不是纯文本

转载 作者:IT王子 更新时间:2023-10-29 00:04:22 25 4
gpt4 key购买 nike

我正在尝试实现这样的东西:

<div>
<input type="checkbox" name="checkbox" id="checkbox_id" />
<label for="checkbox_id">I agree to the <a href="/tos">Terms of Service</a></label>
</div>

我最接近实现的是通过:

<div>
{{ form_widget(form.agreeWithTos) }}
<label for="{{ form.agreeWithTos.vars.id }}">I agree to the <a href="#">Terms of Service</a></label>
</div>

有没有更好的方法?必须指定 {{ form.agreeWithTos.vars.id }} 是不雅的。 :)

最佳答案

在我的表单主题中使用以下代码解决了这个问题:

{# ---- form-theme.html.twig #}
{% block checkbox_row %}
{% spaceless %}
<div>
{{ form_errors(form) }}

<label class="checkbox" for="{{ form.vars.id }}">
{{ form_widget(form) }}
{{ label|default(form_label(form)) | raw }}
</label>
</div>
{% endspaceless %}
{% endblock %}

然后在您的表单模板中您可以使用:

{% form_theme form '::form-theme.html.twig' %}

{{form_row(form.termsOfServiceAccepted, {
'label' : 'I have read and agree to the <a href="#">Terms and conditions</a>'
})
}}

这样,表单主题中的 block 将应用于页面上的任何复选框。如果你还需要使用默认主题,你可以添加一个参数来启用特殊渲染:

{# ---- form-theme.html.twig #}
{% block checkbox_row %}
{% spaceless %}
{% if not useTosStyle %}
{{ parent() }}
{% else %}
{# ... special rendering ... #}
{% endif %}
{% endspaceless %}
{% endblock %}

可以这样使用:

{% form_theme form '::form-theme.html.twig' %}

{{form_row(form.termsOfServiceAccepted, {
'useTosStyle' : true,
'label' : 'I have read and agree to the <a href="#">Terms and conditions</a>'
})
}}

关于php - Symfony2 中的 HTML 表单标签而不是纯文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7551735/

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