嗨,我正努力在Yii captcha实施中尽我所能,但我被卡住了。有奇怪的东西。
1。即使我写得正确,也会显示错误消息。我必须按下“获取新代码”,然后才能运行。看图像:
2。由于某些原因,我猜这个消息错误是由关键事件生成的。我只想在按下提交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 } ?>
答案 0 :(得分:0)
对于用户模型验证规则,我使用以下:
public function rules()
{
return array(
array('isActive, parentId, role, ban, isLegalEntity, organization, discount, paymentDelay, debtLimit, PaymentMethod, ShippingMethod, KnowingSource, scopeOfActivity, ShablonId, agree, Group', 'numerical', 'integerOnly'=>true),
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'on' => 'register'),
...
),
},
如果您尝试相同的操作:array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'on' => 'register')
?
另外,请确保您的控制器中已定义了验证码操作,如下所示:
class UserController extends Controller
{
public function actions()
{
return array(
// captcha action renders the CAPTCHA image displayed on the contact page
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFF2F2F,
),
);
}
....
}