rules models

i had 2 form, register and update form…

in register form, all field was required but in update form field password2 and verifyCode no need…

here is my models rules


public function rules()

	{

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

		// will receive user inputs.

		return array(

			array('username', 'required'),

			array('password', 'required'),

			array('name, email', 'required'),

			array('id_country', 'required'),

			array('password2', 'required','on'=>'register'),

			array('verifyCode', 'required', 'on'=>'register'),

			array('verifyCode', 'captcha', 'allowEmpty'=>!extension_loaded('gd'),'on'=>'register'),

			array('wp_total, wp_counter, status', 'numerical', 'integerOnly'=>true),

			array('id_admin, id_country', 'length', 'max'=>20),

			array('password', 'length', 'max'=>100, 'min'=>3),

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

			array('ip, notes', 'length', 'max'=>255),

			array('name', 'length', 'max'=>160),

			array('email','email','checkMX'=>true),

			array('password2', 'length', 'max'=>50, 'min'=>5),

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

			array('username, email', 'unique'),  

			

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id_developer,id_admin, id_country, date_created, date_last_update, username, password, name, notes, wp_total, wp_counter, ip, status', 'safe', 'on'=>'search'),

		);

	}

with that code, update success but in register i’m be able to save without fill field password2 and verifyCode.

but when i changed


array('password2', 'required','on'=>'register'),

array('verifyCode', 'required', 'on'=>'register'),

to


array('password2', 'required''),

array('verifyCode', 'required'),

register must fill all that field (that’s i want) and in update i can’t save the record because they need field password2 and verifycode…

why it’s because??

how to solve that??

thanks in advance

sorry for my bad english

I see you use ‘register’ for the scenario. May I know where you set it? By default, Yii use ‘insert’ (when creating new record) and ‘update’ (when updating record) scenario.

A quick solve for your problem may be:


array('password2, verifyCode', 'required','on'=>'insert')

ho ho…

thanks agan rei…

but i had problem again…

i have reset password form to reset the password…

but if i use $model->validate(), i can’t save the password. it shows this.

Please fix the following input errors:

Username is too short (minimum is 5 characters).

Email is not a valid email address.

i want username and email just validate in insert and update but not in reset password…

any solution???

thanks…

this my models


public function rules()

	{

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

		// will receive user inputs.

		return array(

			array('username, email, name', 'required', 'on'=>'insert, update'),

			array('password', 'required'),

			array('id_country', 'required'),

			array('password2, verifyCode', 'required','on'=>'insert'),

			//array('verifyCode', 'required'),

			array('verifyCode', 'captcha', 'allowEmpty'=>!extension_loaded('gd'),'on'=>'insert'),

			array('wp_total, wp_counter, status', 'numerical', 'integerOnly'=>true),

			array('id_admin, id_country', 'length', 'max'=>20),

			array('password', 'length', 'max'=>100, 'min'=>5),

			array('username', 'length', 'max'=>100, 'min'=>5),

			array('ip, notes', 'length', 'max'=>255),

			array('name', 'length', 'max'=>160),

			array('email','email','checkMX'=>true),

			array('password2', 'length', 'max'=>50, 'min'=>5),

		//	array('oldpassword', 'length', 'max'=>50, 'min'=>5),

		//	array('oldpassword', 'required','on'=>'reset'),

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

			array('username, email', 'unique'),  

			

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id_developer,id_admin, id_country, date_created, date_last_update, username, password, name, notes, wp_total, wp_counter, ip, status', 'safe', 'on'=>'search'),

		);

	}

in controller


public function actionResetPassword()

	{

		$model=new Developer('reset');

	//	$model->scenario='reset';


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


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

		{

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

			$password = $_POST['Developer']['oldpassword'];

			//$model=$this->loadModel(Yii::app()->user->id_developer);

			$connection=Yii::app()->db;  

			$sql='SELECT password FROM developer where id_developer = '.Yii::app()->user->id_developer;

			$q=$connection->createCommand($sql)->queryAll();

			//print_r($q[0]['password']);exit();

			$pass = $q[0]['password'];

			$password = md5(md5($password).Yii::app()->params["salt"]);

			if ($pass!==$password){

				Yii::app()->user->setFlash('pesan','You enter not valid password.');

				$this->refresh();


			} else {

				if($model->validate()){

				$model->save();

				$this->redirect(array('view','id'=>$model->id_developer));

				}

				}

			}


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

				'model'=>$model,

			));


	}

You just need to add scenario to your validation like (not tested):




array('username', 'length', 'max'=>100, 'min'=>5, 'on'=>'insert, update'),

array('email','email','checkMX'=>true, 'on'=>'insert, update'),



it works…

ty…