我试图使用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)); } } }
我的advertise.php视图中的代码
问题是$model->validate()
当输入正确的代码时返回false.
$model->getErrors()
总是返回'验证码不正确'.