Validation scenario don't work

I’m trying to insert validation rule in my model class, but when I’m trying to save, it always save model if I pass blank firstName, lastName parameters.

User Model




...

public function rules()

{

    return array(

       array('firstName, lastName', 'required', 'message'=>'custom message', 'on'=>'profile-update'), 

       array('firstName, lastName', 'length', 'max'=>30)

    );

}



Controller




function actionUpdate(){

        $form=new User('profile-update');        

        $form = $form->findByPk(Yii::app()->user->id);

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

        {

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

            if($form->validate()){   

                $form->save(false);

            }

        }

        $this->render('update-form',array('user'=>$form));

    }



What I’am doing wrong?

Calling $form->findByPk will (re-)set the scenario to ‘update’.

Maybe should be added as Yii bug/feature request.

So you have to set the scenario after ‘findByPK’




   $form = User::model()->findByPk(Yii::app()->user->id);

   $form->scenario = 'profile-update';


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

   { 

   ...



Thank You! It works :)