2013-03-12 64 views
1

Gregwar验证码错误我安装GregwarCaptcha束和在控制器在模板在symfony中2.1.7

{% form_theme form 'MyIndexBundle:Default:captcha.html.twig' %} 
<form action="" method="post"> 
    ........ 
    {{ form_widget(form.captcha) }} 
    ....... 
    </form> 

添加以下代码

$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() 

        )); 

下面的代码和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,directily我得到captcah码不存在...

+0

是不是递归问题?在应该呈现小部件的块中,您将窗体渲染为参数来调用'form_widget'。 – 2013-03-13 21:45:53

回答

1

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

首先,添加new Gregwar\CaptchaBundle\GregwarCaptchaBundle(),AppKernel.php

然后添加folowwing代码上config.yml

gregwar_captcha: 
    width: 200 
    height: 50 
    length: 6 

然后,添加->add('captcha', 'captcha')$builder

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

所以我没有用form_theme既不captcha.html.twig

,这是工作得很好。

+0

我刚刚尝试过主题。 '{%form_theme form'with':: captcha.html.twig'%}''这也行得通。我注意到,在你的代码中,当使用'form_theme'时,你忘记了“with”。另一个区别是我没有把''''''''''''''''''''''''''''''''''''''''''''''。希望这会帮助你。 – emottet 2013-04-04 10:31:59