Zend验证码问题

时间:2014-05-01 10:09:55

标签: php zend-framework2 captcha

我是初学者,我遵循zend框架网站上的教程,从骨架中创建一个简单的Web应用程序。我正在尝试在教程提出的表单中添加更多元素,特别是'image'capctha,但是,当我尝试在适当的视图中渲染它时,我得到了这个致命的错误:

致命错误:在C:\ xampp \ htdocs \ new \ vendor \ zendframework \ zendframework \ library \ Zend \ Form \ View中调用未定义的方法Zend \ Form \ Element :: getCaptcha() \ Helper \ FormCaptcha.php在第45行。

这是与albumform类相关的代码:

 <?php

    namespace Album\Form;



     use Zend\Form\Element;
     use Zend\Form\Form;
     use Zend\Captcha\Image;
     use Zend\Captcha\AdapterInterface;

     class AlbumForm extends Form
     {
         protected $captcha; 
         public function __construct($name = null)
         {
             // we want to ignore the name passed
             parent::__construct('album');

             $this->captcha = new Image(array(
                'expiration' => '300',
                'wordlen' => '7',
                'font' => 'public/fonts/glyphicons-halflings-regular.tff',
                'fontSize' => '20',
                'imgDir' => 'public/captcha',
                'imgUrl' => '/captcha'
            ));

             $this->add(array(
                 'name' => 'id',
                 'type' => 'Hidden',
             ));
             $this->add(array(
                 'name' => 'title',
                 'type' => 'Text',
                 'options' => array(
                     'label' => 'Title',
                 ),
                 'attributes' => array(
                     'value' => 'Scrivi il titolo',
                     'class' => 'prova',
                 ),
             ));
             $this->add(array(
                 'name' => 'artist',
                 'type' => 'Text',
                 'options' => array(
                     'label' => 'Artist',
                 ),
             ));

              $this->add(array(
                'name' => 'captcha',
                'attributes' => array(
                   'type' => 'Captcha'
                ),
                'options' => array(
                   'label' => 'Please verify you are human.',
                   'captcha' => $this->captcha
                )
             ));

             $this->add(array(
                 'name' => 'submit',
                 'type' => 'Submit',
                 'attributes' => array(
                     'value' => 'Go',
                     'id' => 'submitbutton',
                 ),
             ));

         }
     }

这是视图中的代码:

     <?php
     $form->setAttribute('action', $this->url('album', array('action' => 'add')));
     $form->prepare();

     echo $this->form()->openTag($form) . "\n";
     echo $this->formHidden($form->get('id')) . "\n";
     echo $this->formLabel($form->get('title')) . "\n";
     ?>
     <br/>
     <?php
     echo $this->formInput($form->get('title')) . "\n";
     ?>
     <br/><br/>
     <?php
     echo $this->formLabel($form->get('artist')) . "\n";
     ?>
     <br/>
     <?php
     echo $this->formInput($form->get('artist')) . "\n"; 
     ?>
     <br/><br/>
     <?php
     echo $this->formCaptcha($form->get('captcha')) . "\n"; 
     echo $this->formSubmit($form->get('submit')) . "\n"; 
     echo $this->form()->closeTag() . "\n";
     ?>
你能帮帮我吗?缺少什么?

谢谢!

1 个答案:

答案 0 :(得分:1)

问题在于元素定义。你有:

$this->add(array(
    'name' => 'captcha',
    'attributes' => array(
        'type' => 'Captcha'
    ),
    'options' => array(
        'label' => 'Please verify you are human.',
        'captcha' => $this->captcha
    )
));

但&#39;类型&#39;是在错误的地方。它应该是:

$this->add(array(
    'name' => 'captcha',
    'type' => 'Captcha'
    'options' => array(
        'label' => 'Please verify you are human.',
        'captcha' => $this->captcha
    )
));