How To Validate More Then One Model

Hello,

i have 7 models.i have only one form, where 6 tabs are available. i can only validate single model name "users". how can i validate all models ? [b]PLZ CAN ANY ONE HELP ME…

[/b]

Hi, Shakil

If u want to use the validation rules from various models then follow this >>

In your controller just do like this…





public function actionAction_name()

{

   $model1 = Model1;

   $model2 = Model2;

if(isset($_POST[Model1]),isset($_POST['MOdel2']))

{

 $model1->attributes = $_POST[Model1];


 $model2->attributes = $_POST[Model2];


 $valid = $model1->validate();

 $valid = $model2->validate() && $valid;


if($valid)

{//if valid  

//then code , what you want to do

}

 


}

$this->render('your_view_form_name',array('model1'=>$model1, 'model2'=>$model2));


}




I am taking two models example, you can handle multiple model like the same.

Hi Shakil,

First off, what kind of validation do you want to implement on your Models?

Yii already have a validation in Models and it is under the method rules().

If you want to add another validation, you can code it inside your model or do as what mannu did.

Cheers! ::)

Basically, you’ll probably want in one controller[size=2] to validate the other models. The controller you’ll choose to do all validations in should be the ‘main’ controller and need to be the one that controls the most important or pivotal model. [/size]

[size=2]This is a task you can find plenty of guides on the web to help you with. Here are several of them (note that there are more):[/size]

http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models/

http://www.yiiframework.com/wiki/362/how-to-use-multiple-instances-of-the-same-model-in-the-same-form/

Here below i give u briefly…plz help for this

=>here my only one controller "UserController" where i put all logic for 6 models.

public function actionCreate()

{


	$model=new Users;


            $activity=new Activity;


	 $education=new Education;


	 $trainingtype=new Trainingtype;


	 $training=new Training;


	 $workshop=new Workshop;


	 $others=new Others;


	 $userid=null;


	


	// Uncomment the following line if AJAX validation is needed


	$this->performAjaxValidation($model);


	


	$transaction=$model->dbConnection->beginTransaction();


  


	/*CREATE USERS*/ 


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


	{


		


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





		$images=CUploadedFile::getInstance($model,'image');


		$images->saveAs('./images/'.$images->name);


		$model->image=Yii::app()->baseUrl.'/images/'.$images->name;


		


		if($model->validate())


		{


		


		if($model->save())


		{


		$userid=$model->user_id;


		


		}


		}


				


                    


		                


    }

if(isset($_POST[‘Education’][‘year’]) && isset($_POST[‘Education’][‘cgpa’]) && isset($_POST[‘Education’][‘institute’]))//another model name “Education”

	{


	if(!empty($userid))


	{


	


	$year=$_POST['Education']['year'];


	$cgpa=$_POST['Education']['cgpa'];


	$institute=$_POST['Education']['institute'];


	$status=0;


	foreach($institute as $k=>$v)


	{


	$y=$year[$k];


	$c=$cgpa[$k];


	if(!empty($year) && !empty($cgpa) && !empty($v))


	{


	$education->education_id=null;


	$education->type=$k;


	$education->year=$y;


	$education->cgpa=$c;


	$education->institute=$v;


	$education->user_id=$userid;


	$education->status=$k==2?$_POST['Education']['status']:0;		


	$education->isNewRecord=true;


	$education->save();


	}


	


	}


	}


	


	}

Here my Ajex validation in "UserController"

protected function performAjaxValidation($model)

{


	if(isset($_POST['ajax']) && $_POST['ajax']==='users-form')


	{


		echo CActiveForm::validate($model);


		Yii::app()->end();


	}


}

and i want to validate this in "Education" model…

public function rules()

{

return array(

array(‘year’,‘match’,‘pattern’=>’/^(\d)$/’,‘message’=>‘Invalid Year’),);//here year will put in textfield where only will be combination of [0-9]

}

plzzzzzzzzzz help thisssssssss

i tried which link u r given…but can’t work…plz check i give u details in above.

hi shakil what type of validation u want?

put ur code inside < >





//here the code is well readable




<Here below i give u briefly…plz help for this

=>here my only one controller "UserController" where i put all logic for 6 models.

public function actionCreate()

{

$model=new Users;

$activity=new Activity;

$education=new Education;

$trainingtype=new Trainingtype;

$training=new Training;

$workshop=new Workshop;

$others=new Others;

$userid=null;

// Uncomment the following line if AJAX validation is needed

$this->performAjaxValidation($model);

$transaction=$model->dbConnection->beginTransaction();

/CREATE USERS/

if(isset($_POST[‘Users’]))

{

$model->attributes=$_POST[‘Users’];

$images=CUploadedFile::getInstance($model,‘image’);

$images->saveAs(’./images/’.$images->name);

$model->image=Yii::app()->baseUrl.’/images/’.$images->name;

if($model->validate())

{

if($model->save())

{

$userid=$model->user_id;

}

}

}

if(isset($_POST[‘Education’][‘year’]) && isset($_POST[‘Education’][‘cgpa’]) && isset($_POST[‘Education’][‘institute’]))//another model name “Education”

{

if(!empty($userid))

{

$year=$_POST[‘Education’][‘year’];

$cgpa=$_POST[‘Education’][‘cgpa’];

$institute=$_POST[‘Education’][‘institute’];

$status=0;

foreach($institute as $k=>$v)

{

$y=$year[$k];

$c=$cgpa[$k];

if(!empty($year) && !empty($cgpa) && !empty($v))

{

$education->education_id=null;

$education->type=$k;

$education->year=$y;

$education->cgpa=$c;

$education->institute=$v;

$education->user_id=$userid;

$education->status=$k==2?$_POST[‘Education’][‘status’]:0;

$education->isNewRecord=true;

$education->save();

}

}

}

}

Here my Ajex validation in "UserController"

protected function performAjaxValidation($model)

{

if(isset($_POST[‘ajax’]) && $_POST[‘ajax’]===‘users-form’)

{

echo CActiveForm::validate($model);

Yii::app()->end();

}

}

and i want to validate this in "Education" model…

public function rules()

{

return array(

array(‘year’,‘match’,‘pattern’=>’/^(\d)$/’,‘message’=>‘Invalid Year’),);//here year will put in textfield where only will be combination of [0-9]

}

plzzzzzzzzzz help thisssssssss >

I want ajax validation

Ok fine in your view file check ajax validation is true or not, make it true there

As ur controller…




protected function performAjaxValidation($model)

{

if(isset($_POST['ajax']) && $_POST['ajax']==='users-form')

{

echo CActiveForm::validate($model);

Yii::app()->end();

}

}



I think its not possible its not possible to pass multiple model instances at a time,

SO why u r not trying the solution given by me at first, that is easy to use and secure also.

ajax validation is true… i can only validate $model now, but not others model…is ur first solution really secured???

yes Shakil that is best solution for your problem also as I think, because you are going to use the validation rules of multiple models.

try that …

tnks for ensure…i shall try…