CAPTCHA validate

Hi

I make a register action in site controller like

    public function actionRegister()


    {


        $register = new User;


        $register->scenario = 'register';


        // collect user input data


        if (isset($_POST['User'])) {


            $register->attributes = $_POST['User'];


            $register->scenario = 'register';


            echo $_POST['User']['captchaCode'];


            // validate user input and redirect to previous page if valid


            if ($register->validate()) {


                $register->lastLoginIp = $register->getRealIpAddress();


                $register->lastLoginDate = time();


                $register->password = hash('whirlpool', $_POST['User']['password']);


                $register->save();


                Yii::app()->user->setFlash('Sign Up!','Thank you for signing up. We will send an activation link by email to you as soon as possible.');


                $this->refresh();


            }





        }


        // display the login form


        $this->render('register', array('register' => $register));


    }

and a user class based on class which yiic shell made it for me >>model User from user tabel and I change the rule

	public function rules()


	{


		return array(


			array('username', 'length', 'min' => 3, 'max' => 100, 'on'=>'register'),


			array('password','length', 'min' => 6, 'max' => 128, 'on'=>'register'),


			array('password2','compare','compareAttribute'=>'password', 'on'=>'register'), 


			array('email', 'length', 'max' => 150, 'on'=>'register'),


            array('email', 'email', 'on'=>'register'), 


            array('email2','compare','compareAttribute'=>'email', 'on'=>'register'), 


			array('url', 'length', 'max' => 250, 'on'=>'register'), 


			array('url','url', 'on'=>'register'), 


			array('name', 'length', 'max' => 150, 'on'=>'register'), 


			array('family', 'length', 'max' => 150, 'on'=>'register'), 


			array('address', 'length', 'max' => 250, 'on'=>'register'), 


			array('pobox', 'length', 'max' => 15, 'on'=>'register'), 


			array('telephone', 'length', 'max' => 30, 'on'=>'register'), 


			array('cellphone', 'length', 'max' => 30, 'on'=>'register'),


			array('captchaCode', 'captcha', 'allowEmpty'=>!extension_loaded('gd')), 


			array('username, password, email, name, family', 'required', 'on'=>'register'),


		);


	}

and add some attribute

	public $password2;


	public $email2;


	public $captchaCode;

and in view file make something like this

<?php if (extension_loaded('gd')): ?>


<div class="simple">


<?php echo CHtml::activeLabelEx($register, 'captchaCode'); ?>


	<div>


	<?php $this->widget('CCaptcha'); ?>


	<?php echo CHtml::activeTextField($register, 'captchaCode'); ?>


	</div>


<p class="hint">Please enter the letters as they are shown in the image above.


<br/>Letters are not case-sensitive.</p>


</div>


<?php endif; ?>

but when I submitting the form with correct text for captcha image validator make an error message which The verification code is incorrect.

would you plz help me.

In your Model class add a public property verifyCode (public $verifyCode)

In actionRegister replace the line: "echo $_POST['User']['captchaCode'];" with this one: $register->verifyCode = $_POST["User"]["verifyCode"];

That's it.

Quote

In your Model class add a public property verifyCode (public $verifyCode)

In actionRegister replace the line: "echo $_POST['User']['captchaCode'];" with this one: $register->verifyCode = $_POST["User"]["verifyCode"];

That's it.

tanx mirrorp

but can you explain my problem I know one that is the name of attribute captchaCode should be verifyCode but in register action you assign verifyCode by $register->verifyCode = $_POST["User"]["verifyCode"]; but I think massive assignment assign all variables in $_POST by $register->attributes = $_POST['User']; why we should assign verifyCode again

Hi, ramram,

This happens because you probably extend your model class from CActiveRecord, which fetches all db columns and massively assign them, but verifyCode is not a column in the db table, so this one is not assigned (this is a parameter from CFormModel and not from CActiveRecord) and that’s why you have to assign it manually. I think this is the problem ;) It worked, isn’t it ?

Cheers.

Quote

Hi, ramram,

This happens because you probably extend your model class from CActiveRecord, which fetches all db columns and massively assign them, but verifyCode is not a column in the db table, so this one is not assigned (this is a parameter from CFormModel and not from CActiveRecord) and that’s why you have to assign it manually. I think this is the problem ;) It worked, isn’t it ?

Cheers.

yes it is. tanx a lot