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.

Model




	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');

		}

	}

View


	<div class="form-group">

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

			Current Password

		</label>

		<?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'); ?>

	</div>

Controller


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'])) {


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


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

				$message = array(

								'type' 		=>'success',

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

				$view_data['message'] = $message;

			}

		}


		$view_data['model'] = $model;

		$this->render('editprofile',$view_data);

	}

}




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.

http://www.yiiframework.com/wiki/168/create-your-own-validation-rule/

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