我想将Gregwar Bundle(验证码)添加到我的项目中。我完成了https://github.com/Gregwar/CaptchaBundle/blob/master/README.md
中显示的所有步骤但是,它抛出“变量”captcha_code“不存在”异常。表格主题部分是否有问题?
答案 0 :(得分:2)
使用tag form_theme
示例:
index.html.twig - 包含表单的模板, captcha.html.twig - 带验证码的模板
index.html.twig内容:
{% form_theme form 'YourBundle::captcha.html.twig' %}
<form action="" method="post">
...
{{ form_widget(form.captcha) }}
...
</form>
captcha.html.twig内容:
{% block captcha_widget %}
{% spaceless %}
<img src="{{ captcha_code }}" alt="" title="captcha" width="{{ captcha_width }}" height="{{ captcha_height }}" />
...
{{ form_widget(form, {'attr': { 'autocapitalize': 'off','autocorrect': 'off' }}) }}
...
{% endspaceless %}
{% endblock %}
答案 1 :(得分:0)
块captcha_widget必须在任何其他块之外,例如:
{% block captcha_widget %}
{% set label = label|default('')|trim %}
{% if is_human %}
-
{% else %}
{% spaceless %}
<div class="image">
<img id="{{ image_id }}" src="{{ captcha_code }}" alt="" title="captcha" width="{{ captcha_width }}" height="{{ captcha_height }}" />
{% if reload %}
<script type="text/javascript">
function reload_{{ image_id }}() {
var img = document.getElementById('{{ image_id }}');
img.src = '{{ captcha_code }}?n=' + (new Date()).getTime();
}
</script>
<a class="captcha_reload" onclick="reload_{{ image_id }}(); return false;" href="#">{{ 'Renew'|trans({}, 'gregwar_captcha') }}</a>
{% endif %}
</div>
<div class="">{{ label }}{{ label ? '* :': ''}}{{ form_widget(form) }}</div>
{% endspaceless %}
{% endif %}
{%endblock captcha_widget%}
{%form_theme form2 _self%}
{%block content%} ... {%endblock%}