【问题标题】:Variable "captcha_code" does not exist Gregwar变量“captcha_code”不存在 Gregwar
【发布时间】:2023-03-08 02:51:01
【问题描述】:

每个人我都在尝试使用 Gregwar/CaptchaBundle 在我的表单中添加验证码,但我遇到了一个问题:

当我支付这部分代码时,变量“captcha_code”不存在:

================================================ ==========================

<div class="col-sm-8 col-sm-offset-2">
 {% block captcha_widget %}
 {% spaceless %}
<img src="{{ captcha_code }}" title="captcha" width="{{ captcha_width }}" height="{{ captcha_height }}" />
 {{ form_row(form.captcha) }}
  {% endspaceless %}
 {% endblock %}
  </div>

【问题讨论】:

    标签: forms symfony bundle captcha php-5.3


    【解决方案1】:

    widget块内的变量form已经包含验证码字段(不是整个表单),所以正确的方法是使用form_widget(form)而不是form_row(form.captcha)

    {% block captcha_widget %}
    {% spaceless %}
        <img src="{{ captcha_code }}" title="captcha" width="{{ captcha_width }}" height="{{ captcha_height }}" />
        {{ form_widget(form) }}
    {% endspaceless %}
    {% endblock %}
    

    【讨论】:

      【解决方案2】:

      谢谢你,但我自己想出了解决我的问题的最好方法是

      在我的表单生成器中配置验证码字段

      $builder->add('captcha', 'Gregwar\CaptchaBundle\Type\CaptchaType',array(
                  'width' => 200,
                  'height' => 50,
                  'length' => 6,
                  'quality' => 90,
                  'distortion' => true,
                  'background_color' => [115, 194, 251],
                  'max_front_lines' => 0,
                  'max_behind_lines' => 0,
                  'attr' => array('class' => 'form-control',
                  'rows'=> "6"
                  )
              ));
      

      在我的 twig 模板中只调用这一小部分代码:

      <div class="col-sm-8 col-sm-offset-2">
      {% block captcha_widget %}
          {% spaceless %}
             {{ form_row(form.captcha) }}
      {% endspaceless %}
      
      {% endblock %}
      </div>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-16
        • 2020-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多