【发布时间】:2015-02-05 17:19:29
【问题描述】:
您好,我正在努力实现 Yii 验证码,但我被卡住了。有一些奇怪的东西。
1. 即使我写对了,也会出现消息错误。我必须按“获取新代码”,然后它才能工作。看图片:
2. 我猜出于某种原因,此消息错误是由 on key 事件生成的。我只想在按下提交按钮时生成消息错误,该按钮执行 ajax 请求。
这是我的模型:
public $verifyCode;
....
public function rules() {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
if ($this->scenario == "insert") {
return array(
array('requisition_id, sync', 'numerical', 'integerOnly' => true),
array('lastname, firstname, email, dob, phone, cv_path, experienceMonths, experienceYears, competencies, token', 'required', 'message' => "Câmpul este obligatoriu"),
array('email', 'email', 'message' => "Emailul este invalid!"),
array('dob', 'validateDob'),
array('dayOfBirth, monthOfBirth, yearOfBirth', 'safe'),
array('taleo_id, sync', 'required', 'on' => 'taleoUpdate'),
array('verifyCode', 'captcha', 'captchaAction'=>'site/captcha')
// The following rule is used by search().
// Please remove those attributes that should not be searched.
);
} else if ($this->scenario == 'taleoUpdate') {
return array(
array('taleo_id, sync', 'required'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
);
}
else if($this->scenario == 'notjobapply'){
return array(
array('lastname, firstname, email, phone, cv_path, requisition_id', 'required', 'message'=>'Câmpul este obligatoriu'),
array('email', 'email', 'message' => "Emailul este invalid!"),
);
}
}
这是控制器
public function accessRules()
{
return array(
array('allow',
'actions'=>array('aplica', 'captcha'),
'users'=>array('*'),
),
);
}
....
$this->render('apply', array(
"applicant" => $model,
'job' => $job,
'region' => $region,
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFFFFFF,
),
));
最后是视图:
<?php
if(CCaptcha::checkRequirements()){ ?>
<?php echo $form->labelEx($applicant,'verifyCode'); ?><br>
<?php $this->widget('CCaptcha', array('captchaAction'=>'site/captcha')); ?> <br><br>
<?php echo $form->textField($applicant,'verifyCode'); ?><br>
<?php echo $form->error($applicant,'verifyCode'); ?><br><br>
<?php } ?>
【问题讨论】: