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

发布于 2024-12-22 00:08:46 字数 2502 浏览 2 评论 0原文

我需要使用 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 ?>

它显示以下内容:

在此处输入图像描述

即它显示 2 个文本框。

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

I need to use Zend Captcha and wrote the follwoing code:

<?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'))));

        }

    }

?>

The problem I encounter is that when I display it in my "form.phtml" file like:

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

It displays the following:

enter image description here

i.e. It is displaying 2 textboxes.

Kindly help me in counteracting this situation. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞别 2024-12-29 00:08:46

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

You're getting the second textfield because you're using the ViewHelper decorator for the captcha element. Don't set the ViewHelper decorator for the captcha element and it should work.

巷雨优美回忆 2024-12-29 00:08:46

您好,我正在使用此代码来删除额外的字段,

$captcha = new Zend_Form_Element_Captcha('captcha',array('label' => 'Write the chars to the field',  
                                                            'captcha' => array(
                                                                'captcha' => 'Image',   
                                                                'wordLen' => 3,
                                                                'timeout' => 300,
                                                                'font' => APPLICATION_PATH.'/../public/font/two.ttf', 
                                                                'imgDir' => APPLICATION_PATH.'/../public/captcha/', 
                                                                'imgUrl' => 'http://localhost/projx/public/captcha/'

                                                            ),
                                            'decorators' => $this->elementDecorators,
                                            ));   
                                            $captcha->removeDecorator('ViewHelper');

这是我的装饰器

 'decorators' => $this->elementDecorators,

,这是默认的装饰器,

我只是使用以下代码删除 ViewHelpre

$captcha->removeDecorator('ViewHelper');

我希望这对您有用...:)

hi i am using this code to to remove extra field

$captcha = new Zend_Form_Element_Captcha('captcha',array('label' => 'Write the chars to the field',  
                                                            'captcha' => array(
                                                                'captcha' => 'Image',   
                                                                'wordLen' => 3,
                                                                'timeout' => 300,
                                                                'font' => APPLICATION_PATH.'/../public/font/two.ttf', 
                                                                'imgDir' => APPLICATION_PATH.'/../public/captcha/', 
                                                                'imgUrl' => 'http://localhost/projx/public/captcha/'

                                                            ),
                                            'decorators' => $this->elementDecorators,
                                            ));   
                                            $captcha->removeDecorator('ViewHelper');

this is my decorator

 'decorators' => $this->elementDecorators,

this is default decorator

i just remove ViewHelpre using followig code

$captcha->removeDecorator('ViewHelper');

i hope this will work for you... :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文