脚本宝典收集整理的这篇文章主要介绍了php – yii captcha无法正确验证,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。@H_406_0@ 我试图使用yii添加验证码到我的联系表单,但验证有一些问题.
我的模特
class ContactForm extends CFormModel { public $verifyCode; public function rules() { return array( array('verifyCode','captcha','allowEmpty'=>!CCaptcha::checkRequirements(),'on'=>'captcharequired'),array('verifyCode','safe'),); } }
我控制器中的代码
public function filters() { return array( 'accessControl',); } public function accessRules() { return array( array( 'allow',//allow all users to PErform advertise and index action 'actions' => array('advertise','index','captcha'),'users' => array('*'),),); } public function actions() { return array( // captcha action renders the CAPTCHA image displayed on the contact page 'captcha' => array( 'class' => 'CCaptchaAction','backColor' => 0xFFFFFF,'testLimIT'=> '2',) } public actionAdvertise() { $model = new ContactForm; $model->scenario = 'captcharequired'; if($model->validate()){ //some code } else { $this->render('advertise',array('model' => $model)); } } }
<form action="" method="post"> <?PHP $form=$this->beginWidget('CActiveForm',array( 'id'=>'contact-form','enableAjaxValidation'=>false,)); ?> <?PHP if(CCaptcha::checkRequirements()){ ?> <div class="row"> <div class="contact_field_wrapper"> <?PHP echo '<b>ARE YOU HUMAN?</b><br />'.$form->labelEx($model,'verifyCode'); ?> <div class="captcha user-captcha"> <?PHP $this->widget('CCaptcha',array( 'captchaAction'=>'site/captcha' )); ?> <?PHP echo $form->error($model,'verifyCode'); ?> <?PHP echo '<br />'.$form->textField($model,'verifyCode'); ?> <div class="hint">Please enter the letters as they are shown in the image above.<br/> Letters are not case-sensitive. </div> </div> </div> </div> <?PHP } ?> <?PHP $this->endWidget(); ?> </form>
问题是$model-> validate()在输入正确的代码时返回false.
$model-> getErrors()始终返回’验证码不正确’.
做这个 :
if($_POST['ContactForm']) { $model->attributes() = $_POST['ContactForm']; if($model->validate()) { //some code } } $this->render('advertise',array('model' => $model));
以上是脚本宝典为你收集整理的php – yii captcha无法正确验证全部内容,希望文章能够帮你解决php – yii captcha无法正确验证所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。