php – Zend_Form Captcha没有验证
我已经阅读了很多关于此的帖子,但我无法解决我的问题.
当我尝试验证我的zend表单验证码时,即使使用正确的文本也总是失败. 这是我的代码: //我在哪里打电话给我的表格 public function contactAction() { $this->view->form = new Forms_ContactForm(); } //我的表格 class Forms_ContactForm extends Twitter_Form { public function init() { $this->setAction( 'email/email/contact' ); $this->setMethod('post'); $this->addElement('text','strFirstName',array( 'required' => true,'label' => 'Your First Name' ) ); $this->addElement('text','strLastName','label' => 'your Last Name' ) ); $this->addElement('text','strEmail',array( 'validators' => array( array('EmailAddress') ),'required' => true,'label' => 'Your Email' ) ); $this->addElement('textarea','strContent','label' => 'Your message' ) ); $this->addElement('captcha','captcha',array( 'label' => 'Please enter the 5 letters displayed below:','required' => true,'name' => 'captchaField','captcha' => 'image','captchaOptions' => array( 'captcha' => 'image','font'=> 'static/font/arial.ttf','imgDir'=>'static/img/captcha','imgUrl'=> 'static/img/captcha/','wordLen' => 5,'fsize'=>20,'height'=>60,'width'=>200,'gcFreq'=>50,'expiration' => 300) )); $this->addElement('submit','submit',array('class' => 'Submit') ); } } //以及我发送它的动作 public function contactAction() { if( $this->_request->isPost() ) { $objForm = new Forms_ContactForm(); if( $objForm->isValid($_POST) ) { $arrParams = $this->_request->getParams(); if( $arrParams['strFirstName'] && $arrParams['strLastName'] && $arrParams['strEmail'] && $arrParams['strContent'] ) { $this->_objEmail = new Zend_Mail(); $this->_objEmail ->setFrom( $arrParams['strEmail'] ); $this->_objEmail ->setSubject( 'C' ); $this->_objEmail ->setBodyHtml( 'Message from: '. $arrParams['strFirstName'] . ' ' . $arrParams['strLastName'] . '<BR>eMail address: ' . $arrParams['strEmail'] . '<BR><BR>' . $arrParams['strContent'] ); $this->_objEmail ->addTo( 'mail@gmail.com' ); $this->view->bolSent = $this->_objEmail->send(); } } else $this->view->form = $objForm; } } 似乎在我的contactAction中,它会生成一个新的验证码,这就是为什么它与我提交的代码完全匹配,但我不知道如何修复它. 感谢您的时间和帮助! 刚刚看到一些狡猾的东西:当我在接触动作中转储我的$_POST时,我的结果是: array 'strFirstName' => string 'fghjfghj' (length=8) 'strLastName' => string 'ffffffff' (length=8) 'strEmail' => string 'fvhkbno@biu.fr' (length=14) 'strContent' => string 'fewfew' (length=6) 'captchaField' => string 'cebfe69ead38dba86a6b557dc8853b24' (length=32) 我刚刚输入的验证码甚至出现了,而我有验证码kay !! ?? 编辑 再次感谢您的重播! <div class="control-group"> <label class="control-label required" for="captchaField-input">Please enter the 5 letters displayed below:</label> <div class="controls"> <img width="200" height="60" src="static/img/captcha/ab2d15044a637338064b39cfd2675837.png" alt=""> <input type="hidden" id="captchaField-id" value="ab2d15044a637338064b39cfd2675837" name="captchaField[id]"> <input type="text" value="" id="captchaField-input" name="captchaField[input]"> <input type="text" value="ab2d15044a637338064b39cfd2675837" id="captchaField" name="captchaField"> </div> </div> 当我查看我的params发送时,这是我得到的: captchaField ab2d15044a637338064b39cfd2675837 似乎cptchaField重叠[id]和[输入] 我觉得我需要删除这个captchaField,但不知道到目前为止! 我可以用JS做到这一点,但必须有一个干净的方法来做到这一点! 再次编辑 我使用ajax提交表单,序列化.这可能是问题,看看有点不对劲. 编辑TER 它不是由ajax引起的. <input type="text" value="ab2d15044a637338064b39cfd2675837" id="captchaField" name="captchaField"> 使用firebug,一切正常,验证码验证良好.现在的问题是如何正确删除这一行… 解 经过艰苦的努力,这里是解决方案(删除装饰)! $captcha = new Zend_Form_Element_Captcha('captchaField',array('label' => "Please enter the 5 letters displayed below:",'required'=>true,'captcha' => array( 'captcha' => 'image','expiration' => 300 ) )); $this->addElement($captcha); $this->getElement('captchaField')->removeDecorator("viewhelper"); 解决方法
经过艰苦的努力,这里是解决方案(我只需要删除装饰器)!
$captcha = new Zend_Form_Element_Captcha('captchaField','captcha' => array( 'captcha' => 'image','expiration' => 300 ) )); $this->addElement($captcha); $this->getElement('captchaField')->removeDecorator("viewhelper"); 感谢Haroon的帮助和你的时间:) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |