Two modeled form not saving.

Hi all,

I have followed the Cookbook post ( to include two models into the one form. The relationship between the two models is a many to many.

I believe I have followed it correctly and the form presents all the fields as I expected but when I submit the form the page just reloads. There is no error message and no new records are added to the database. I’m a bit unsure what to look for.

Can anyone offer any suggestions on what I can do to find what is wrong?

I am using yii-1.1.2.r2086.



OK, I have resolved this initial issue. The problem was that I had only included on field from the second model and this field had one multi-select input for which I had not selected any options. Therefore Firefox was not sending any $_POST vars for that model. Selecting at least on value would correct the problem. As the example shows, having two models in one form requires the following in the actionCreate() controller.

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

My issue now is that with all fields having valid values I am getting this error:

Please fix the following input errors:

    * User cannot be blank.

    * Business must be an integer.

User and Business are the two models and not actual fields within these models. Does anyone have any ideas of what may be wrong here?

I have used this…i have created the form with 2 models

but i have problem with the redirect…when i pass 2 model…


HOw can i pass $a and $b my 2 model…???

Can you help me???

you can pass the parameters like that:

$this->redirect(array('view','idA'=>$a, 'idB'=>$b->ACCESSO_ID));

And in the view you will have the $_GET like this pseudocode:

$_GET=array('idA'=>$a, 'idB'=>$b->ACCESSO_ID));

Hi guys,

I have also followed the above example…

How do you get the actionUpdate to update modelA and modelB?

if the save section…I tried

public function actionCreate()


$a=$this->loadModel A;

$b=$this->loadModel B;

if(isset($_POST['A'], $_POST['B']))


    // populate input data to $a and $b



    // validate BOTH $a and $b


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



        // use false parameter to disable validation



        // ...redirect to another page



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





Model A and Model B validates…

Model A updates fine…but the problem I have is…Model B is not updating :(

When I check the var_dumb for $b after new data is entered - I notice that old data is still being passed back to db

How can I get Model B to update and save as well?

Many thanks

You may not be setting the relative ID. Have a look at my finished code below which I think will help.

	public function actionCreate()


		$model=new User;

		$UserExtended = new UserExtended;

		// Uncomment the following line if AJAX validation is needed

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

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






				$UserExtended->uId = 1; // Set dummy value to validate.




					$UserExtended->uId = $model->id;












@ dubby,

thanks for your response ;)

I have since resolved this issue. I do have one question though:

Has anyone managed to get two modelled form validated via ajaxValidation?

If so how would you write the validation for both model?

I tried;



When I tried the above only one model managed to validate via ajax successfully, the other failed…

Many thanks in advance for any hints



Hi Seal,

I have not attempted any Ajax form submission yet but I hope to shortly. If you do resolve the issue can you please post your findings here.



Dubby, will do that ;)

Seal, could you post your code validating ajax? I want ajax validate 2 models too and can’t validate even one of them.

I have a problem as well with this functionality, But I am not as advanced as you :

I have a relation between model A and model B : A BELONGS_TO B

I write the method actionRegister in A’s controller :

$model=new A;

$company = new B;


if(isset($_POST['A'], $_POST['B']))


			// populate input data to $a and $b



			// validate BOTH $a and $b


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


	echo "SAVED";






in my rules in A, I have :


and in the B’s model , I don’t have anything related to the A’s register action.

When I run the page, I got this error :

Property "CRequiredValidator.0" is not defined.

I have no idea on how to solve it, I tried to create validators specially for model B, and pass it into actionRegister lile that :

$model=new A('register');

and to combine all possibilities ( remove all validators concerning the actionRegister function ) and experimenting

B rules in B model file :


//or in A's model


with no luck, any ideas on how to get the form displaying?

I have managed to get ajax Validation for 2 models when using an activeForm.

//preform ajax validation

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


 echo CActiveForm::validate(array($model1, $model2));



This syntax might not apply for your situation, but you may have some luck with

$this->performAjaxValidation(array($modelA, $modelB));

It didnt work because after the first validation it triggers Yii::app()->end() in the previous post

I don’t understand is how to input 2 models in _form.php

One nice guy show me this code, I have apply it, but the result is just only $model form is show up but not the $model2, even submit button is not show up too.

and I try to fixed it according tabular input article like this

what I know this about tabular input, but I have strange feeling this is not have any relation with insert data to 2 model article.

what wrong?

I haven’t read the article yet or currently have time to look over your questions but a new article was written on multiple models in a form in the last few days:

I hope this helps.

thanks man, it owesome and solved my problem, just another view custom code in question mark, but I think I have to ask it in the appropriate room.