Custom Validation Not Working

If i specify the scenario the custom validation doesn’t work on current password. It doesn’t work in both of the scenarios. Why is that so. Although in this case i don’t need the scenario was curious why this didn’t work.


	public function rules()


		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('title, name, currentpassword', 'required', 'on'=>'editprofile'),

			array('currentpassword, password, confirmpassword', 'required', 'on'=>'changepassword'),

			array('name, email, password, currentpassword', 'length', 'max'=>255, 'min'=>6),

			array('currentpassword', 'compareCurrentPassword'),

			//array('currentpassword', 'compareCurrentPassword','on'=>array('changepassword, editprofile')),

			array('status', 'safe','on'=>array('recover','signup','resetpassword')),

			array('created, modified', 'safe')



	public function compareCurrentPassword($attribute,$params)


		$User = User::model()->findByAttributes(array('id'=>Yii::app()->User->getId()));

		if (sha1($this->currentpassword) !== $User->password)


			$this->addError($attribute,'Invalid Password');




	<div class="form-group">

		<label for="User_password" class="required">

			Current Password


		<?php echo $form->passwordField($model, 'currentpassword', array('class'=>'form-control','required'=>'required', 'value'=>'', 'maxlength'=>'40', 'pattern'=>'[a-zA-Z0-9-]{6,40}', 'title'=>'Password should be 6-40 characters containing a-z and 0-9')); ?>

		<?php echo $form->error($model, 'currentpassword'); ?>



public function actionEditProfile()


		$this->layout = (Yii::app()->request->isAjaxRequest) ? '//layouts/ajax' :  '//layouts/column2';

		$model = User::model()->findByPk(Yii::app()->User->getId());

		$model->scenario = 'editprofile';

		$view_data  = array();

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


			if ($model->save(true)) {

				$message = array(

								'type' 		=>'success',

								'message'	=>'Account Details Changed.');

				$view_data['message'] = $message;



		$view_data['model'] = $model;




if (sha1($this->currentpassword) != $User->password)


                        $this->addError($attribute,'Invalid Password');


Not Equal " != "

That is correct.It is not equalto only. The whole code works fine if i remove the scenario. It throws the error but not if i add the scenario

okay. then its looks like everything fine.

Check without the comparison, add an error only in the validate function.

I implemented it using this article only but no mention of senarios