php - 如何在 Zend Form 中添加验证码?

标签 php zend-framework zend-form captcha

我需要使用 Zend Captcha 并编写了以下代码:

<?php

    require_once ('Zend/Form.php');

    class Form_Signup extends Zend_Form {

        public function __construct( $options = null ) {

            parent::__construct( $options );

            // Set method
            $this->setMethod('post');

            // Elements array
            $elements = array();

            $element = new Zend_Form_Element_Captcha('captcha', array('label'   => "",
                                                                      'captcha' => array('captcha' => 'Image',
                                                                                         'name'    => 'myCaptcha',  
                                                                                         'wordLen' => 5,  
                                                                                         'timeout' => 300,  
                                                                                         'font'    => 'font/monofont.ttf',  
                                                                                         'imgDir'  => 'captcha/',  
                                                                                         'imgUrl'  => '/captcha/')
                                                                     )
                                                    );
            $elements[] = $element;

            // Submit Button
            $element = $this->CreateElement('submit', 'Signup');
            $element->setLabel('Signup');
            $elements[] = $element;

            // --------------------------
            // Add elements to the form
            // --------------------------

            // update tabindex
            foreach ($elements as $index => $element) {
                $element->setAttrib('tabindex', ($index + 1));
            }

            $this->addElements($elements);
            $this->setElementDecorators(array('ViewHelper'));

            // Set form decorator (what script will render the form)
            $this->setDecorators(array(array('ViewScript' , array('viewScript' => 'signup/form.phtml'))));

        }

    }

?>

我遇到的问题是,当我在“form.phtml”文件中显示它时,如:

<?= $this->element->captcha ?>

它显示以下内容:

enter image description here

即它显示 2 个文本框。

请帮助我应对这种情况。 :)

最佳答案

您将获得第二个文本字段,因为您正在为验证码元素使用 ViewHelper 装饰器。不要为验证码元素设置 ViewHelper 装饰器,它应该可以工作。

关于php - 如何在 Zend Form 中添加验证码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8576437/

相关文章:

zend-framework - Zend -> 警告 : is_readable() [function. 可读]:open_basedir 限制生效

php - 将base64位编码的字符串保存到php中的文件

php - Zend 框架 : Require one out of three values

jquery-ui - Zend 装饰器 : How can I use jquery-ui classes on my forms?

PHP - 如何组合三个或更多相互依赖的类?

php - Laravel - 更新内容错误 Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException

php - 通用编程 : How to implement model logic in MVC?

php - 从 Zend InputFilter 检索错误消息

php - 替换字符串中的 utf8 文字

php - Laravel:用于不同公告类型的单个或多个 mysql 表