problem vith validation + data saving

Hi , i have problem with validation but cant understand what the problem is ,

i have problem in password change file ,

i use following code in view file , here PASSWORD AND CPASSWORD are not db field ( in db i want update field name e_password )


 <div class="row">

    	<?php echo $form->labelEx($model,'PASSWORD'); ?>

    	<?php echo $form->passwordField($model,'PASSWORD'); ?>

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

	</div>

    

     <div class="row">

    	<?php echo $form->labelEx($model,'CPASSWORD'); ?>

    	<?php echo $form->passwordField($model,'CPASSWORD'); ?>

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

	</div>

as these are extra fields i use following things in model file ,


public $PASSWORD;  // declare fields in class of model 

public $CPASSWORD;


array('E_NAME, .... , PASSWORD , CPASSWORD', 'required'),

array('PASSWORD', 'compare', 'compareAttribute'=>'CPASSWORD' ,'message'=>'Password and Confirm Password do not match'),           //  for password compare 

now i use following code in action controller file ,




...

...

if(isset($_POST['Employee']))

		{

		      $newPass = $_POST['Employee']['PASSWORD'];

		

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

			

			if($model->validate())

			{

				

				if($model->changePass($id,$newPass))  // function for update password

				{		

					Yii::app()->user->setFlash('success',"Your account updated successfully.");	

$this->redirect(array('account'));

				}		

		   	}

		$this->render('account',array(

			'model'=>$model,

			'empData'=>$empData,

		));

	}

=> now the problem is when i use if($model->validate()) it validates but data not store in database , i.e condition is not satisfied .

– when i remove if($model->validate()) , the data saved but error not shown …

=> and another big problem is when i use this fields ( PASSWORD AND CPASSWORD ) WHICH ARE NOT IN DB , the other pages stop working like create employee … ( might be problem due to extra field ) …

in this controller just add a $model->save() after the change password.

To have the model working correctly in both cases, use scenarios.

Take a look here.

now its work , but I found problem ,

the problem was when i give extra fields to model like CPASSWORD ( not in database ),

when i post my form it post only two fields PASSWORD and CPASSWORD , but controller can’t validate using $model->validate() because it validating other fields ,

now the solution is i give scenario in model as ,


array('PASSWORD','required', 'on'=>'change'),

array('CPASSWORD','required', 'on'=>'change'),

and the main thing is , we have to give scenario to other fields also , other it will check validation for that also , so give scenario to other fields as ,


array('E_NAME, E_STATE, E_CITY, ... ... ', 'required','on'=>'insert'),

then it woks :)