2

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>
Community
  • 1
  • 1
juhojo
  • 43
  • 5
  • Make sure that the model gets the value into `verifyCode` after `$model->attributes = $request->getPost('RegisterForm');` – SiZE May 31 '17 at 19:22
  • Yep I did test that and the value that client submits is correct. After spending a ton of time (approximately 20 hours) of looking into the documentation of Yii, I decided to throw the Yii captcha validation to garbage bin and go with Google's reCAPTCHA instead. I will leave this question up however, as I was unable to resolve it and since most of the questions regarding the subject offer solutions that did not yield results in this case. – juhojo Jun 05 '17 at 10:51
  • have you tried by putting verifyCode in safe ? i.e `array(verifyCode', 'safe')` – Jigar7521 Sep 26 '17 at 10:37
  • In your view file here is the problem ` if(CCaptcha::checkRequirements()):` , you are checking conditional wise, but model will always validate for captcha value, you have to put conditional validation rule over there in model. – Jigar7521 Sep 26 '17 at 10:44

0 Answers0