如何在Zend Form中添加验证码?
我需要使用 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:
i.e. It is displaying 2 textboxes.
Kindly help me in counteracting this situation. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将获得第二个文本字段,因为您使用了验证码元素的 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.
您好,我正在使用此代码来删除额外的字段,
这是我的装饰器
,这是默认的装饰器,
我只是使用以下代码删除 ViewHelpre
我希望这对您有用...:)
hi i am using this code to to remove extra field
this is my decorator
this is default decorator
i just remove ViewHelpre using followig code
i hope this will work for you... :)