Using Two Model Fields In One Form

In my application have two models user and address. The data saved to two tables using one form. But for validation purpose, only one model form fields are validated if all fields are empty. Means if we just submit the form without enter any data in form, the error message (like "City cannot be blank") happens only for one model. i want to display error for both models.

i checked these posts.



// Validate all three model


[/color] [color="blue"]$valid[/color]=[color="blue"]$[size="2"]model[/size][size="2"]_2[/size][color="#808080"][size="2"]->[/size][/color][color="green"][size="2"]validate[/size][/color][color="olive"]size="2"[/size][/color][color="#808080"][size="2"] && [/size][/color][size="2"]$valid[/size][color="#808080"][size="2"];[/size][/color]

[/color] [color="blue"]$valid[/color]=[color="blue"]$model[/color]->[color="green"]validate[/color]color="olive"[/color] && [color="blue"]$valid[/color];

Thanks…it helps to me.

But have another problem. In the 1st model, id is the foreign key. So if we validate the both fields, the id in the 2nd model not set or blank in the form. so cannot save data in the fields. When it solve?

Dear Friend

We can do this in the following way.


$valid=$model_2->validate(array('attribute1','attribute2','attribute3'....)) && $valid; //you can exclude FK attribute here.