I am using Yii 1.1 with some bootstrap.widgets
. I am trying to implement a CAPTCHA into my registration form using Yii's CCaptcha
with CCaptchaAction
.
Currently I have a UserController
which deals with the rendering of the CAPTCHA image and sets the accessRules
so that CAPTCHA may be used. The RegisterForm
is a model in which I have set a rule that verifyCode
is validated against CAPTCHA-type and that it may not be empty. Lastly Register
is my view and it simply renders the form using TbActiveForm
provided by a widget. According to YiiBooster's documentation TbActiveForm
is an extended version of CActiveForm
, so ajaxValidation
should be disabled which I have done.
I am struggling with the fact that the validation always fails: the input for the verifyCode
-field is correct so I assume the reason for the validation failure is that I am not comparing it to the CAPTCHA that is displayed in the form. However, I am not sure of this and I do not know how to test it. This is the error I am getting:
array(1) { ["verifyCode"]=> array(1) { [0]=> string(35) "The verification code is incorrect." } }
The answer to this question did not help me.
UserController.php
class UserController extends Controller
{
/**
* Declares external action controller
*/
public function actions()
{
return array(
// captcha action renders the CAPTCHA image displayed on the register page
'captcha' => array(
'class' => 'CCaptchaAction',
'backColor' => 0xFFFFFF
)
);
}
/**
* Specifies the access control rules.
* This method is used by the 'accessControl' filter.
* @return array access control rules
*/
public function accessRules()
{
return array(
array(
'allow',
'actions' => array(
'register',
'captcha'
)
)
);
}
/**
* Register new user
*/
public function actionRegister()
{
$model = new RegisterForm(); // Create new register-form
$request = request();
if ($request->isPostRequest) {
$model->attributes = $request->getPost('RegisterForm');
if (!$model->validate()) {
var_dump($model->getErrors());
}
if ($model->validate()) {
var_dump('Got here, so it works!');
}
}
$this->render('register', array('model' => $model));
}
}
RegisterForm.php
class RegisterForm extends CFormModel
{
public $verifyCode;
public function rules()
{
return array(
array('verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements())
);
}
/**
* Declares attribute labels.
*/
public function attributeLabels()
{
return array(
'verifyCode' => 'Verification Code',
);
}
}
Register.php
<div>
<?php
$form = $this->beginWidget(
'bootstrap.widgets.TbActiveForm',
array(
'id' => 'register-form',
'layout' => TbHtml::FORM_LAYOUT_HORIZONTAL
)
);
?>
<fieldset>
<?php
if(CCaptcha::checkRequirements()):
echo $form->labelEx($model, 'verifyCode');
$this->widget('CCaptcha');
echo $form->textField($model, 'verifyCode', array(), false, false);
echo $form->error($model, 'verifyCode', array(), false, false);
endif;
?>
</fieldset>
<?php
echo TbHtml::submitButton(
t('user', 'Register'),
array(
'id' => 'submit-button',
'color' => TbHtml::BUTTON_COLOR_PRIMARY,
'size' => TbHtml::BUTTON_SIZE_LARGE,
'block' => true
)
);
$this->endWidget();
?>
</div>