Yii показать капчу после N неудачных попыток

MODEL LoginForm.php:


public function rules()
{
return array(
// username and password are required
array('username, password', 'required'),
// rememberMe needs to be a boolean
array('rememberMe', 'boolean'),
// password needs to be authenticated
array('password', 'authenticate'),
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(), 'on'=>'captchaRequired'),
);
}

VIEWS login.php:


if(CCaptcha::checkRequirements()){
if($model->scenario == 'captchaRequired'){
echo $form->captchaRow($model,'verifyCode',array(
'hint'=>'Пожалуйста, введите буквы, показанные на картинке выше. Буквы вводятся без учета регистра.',
));
}
}

CONTROLLER SiteController.php:


public $attempts = 3;
public $counter;

private function captchaRequired()
{
return Yii::app()->session->itemAt('captchaRequired') >= $this->attempts;
}

public function actionLogin()
{
$model = $this->captchaRequired()? new LoginForm('captchaRequired') : new LoginForm;

if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}

if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
if($model->validate() && $model->login())
$this->redirect(Yii::app()->user->returnUrl);
else
{
$this->counter = Yii::app()->session->itemAt('captchaRequired') + 1;
Yii::app()->session->add('captchaRequired',$this->counter);
}
}
$this->render('login',array('model'=>$model));
}