Currently I'm doing a log-in form with behaviour similar to GMail's. That means - the CAPTCHA appears only on unsuccessful log-in.
First of all, I have tested the CAPTCHA - it works. Then I added a new variable to the CFormModel:
public $verifyCodeRequired = false;
I have changed the action to look like this:
public function actionLogin()
{
$model=new LoginForm;
// collect user input data
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->login())
{
$this->redirect(Yii::app()->user->returnUrl);
}
[b]$model->verifyCodeRequired = true;[/b]
}
// display the login form
$this->render('login', array('model'=>$model));
}
There is a validation rule:
array('verifyCode', 'captcha', 'allowEmpty'=>!$this->verifyCodeRequired),
So, the verifyCodeRequired gets a value TRUE, but the validator still passes the field when it's empty. I have also tried making a custom validator:
array('verifyCode', 'validateCaptcha')
public function validateCaptcha()
{
var_dump($this->verifyCodeRequired);
if($this->verifyCodeRequired)
{
$validator = new CCaptchaValidator();
$validator->attributes = array("verifyCode");
return $validator->validate($this);
}
else {
return true;
}
}
I always get FALSE. How should I achieve desired behaviour?
EDIT: Nevermind, I understood how to achieve that
Kind regards,
Darwell

Help














