【问题标题】:Gregwar Captcha error in symfony 2.1.7symfony 2.1.7 中的 Gregwar Captcha 错误
【发布时间】:2013-04-04 17:37:55
【问题描述】:

我安装了 GregwarCaptcha 包并在控制器中添加了以下代码

$form = $this->createFormBuilder()
                ->add('captcha', 'captcha', array(
                            'width' => 200,
                            'height' => 50,
                            'length' => 6,
                        ));
    return $this->render('MyIndexBundle:Default:contact.html.twig',array(
                      'form' => $form->createView()

                    ));

模板中的以下代码

{% form_theme form 'MyIndexBundle:Default: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 %}

我收到“对象“Symfony\Component\Form\FormView”不存在的方法“验证码”错误。 ...如果我使用 captcha.html.twig,我会直接得到验证码不存在...

【问题讨论】:

  • 递归不是问题吗?在应该呈现小部件的块中,您调用 form_widget 并将表单呈现为参数。

标签: php captcha symfony-2.1


【解决方案1】:

我管理使用 gregwar 验证码,这样使用它:

首先,将new Gregwar\CaptchaBundle\GregwarCaptchaBundle(), 添加到您的AppKernel.php

然后在config.yml上添加以下代码

gregwar_captcha:
  width: 200
  height: 50
  length: 6

然后,将-&gt;add('captcha', 'captcha') 添加到您的$builder

最后,在树枝模板中添加{{ form_widget(form.captcha) }}

所以我没有使用form_theme 也没有使用captcha.html.twig

这工作得很好。

【讨论】:

  • 我刚刚尝试过主题化。 {% form_theme form with '::captcha.html.twig' %} 这也有效。我注意到在您的代码中,您在使用form_theme 时忘记了“with”。另一个区别我没有把, {'attr': { 'autocapitalize': 'off','autocorrect': 'off' }}放在主题上。希望对您有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多