ani
(Aneesh)
December 25, 2012, 2:30pm
1
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.
http://www.yiiframework.com/wiki/218/how-to-use-single-form-to-collect-data-for-two-or-more-models-cactiveform-and-ajax-validation-edition/
http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models/
kiran123
(Sharmakiran71)
December 25, 2012, 4:38pm
2
// Validate all three model
[color="blue"]$valid[/color]=[color="blue"]$[size="2"]model[/size][size="2"]_1[/size][color="#808080"][size="2"]->[/size][/color][color="green"][size="2"]validate[/size][/color][color="olive"][size="2"]([/size][/color][color="olive"][size="2"])[/size][/color][color="#808080"][size="2"];[/size][/color]
[/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];
ani
(Aneesh)
December 26, 2012, 5:43am
3
// Validate all three model
[color="blue"]$valid[/color]=[color="blue"]$[size="2"]model[/size][size="2"]_1[/size][color="#808080"][size="2"]->[/size][/color][color="green"][size="2"]validate[/size][/color][color="olive"][size="2"]([/size][/color][color="olive"][size="2"])[/size][/color][color="#808080"][size="2"];[/size][/color]
[/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?
seenivasan
(Chellamnivas)
December 26, 2012, 12:46pm
4
Dear Friend
We can do this in the following way.
$valid=$model_1->validate();
$valid=$model_2->validate(array('attribute1','attribute2','attribute3'....)) && $valid; //you can exclude FK attribute here.
if($valid)
{
$model_1->save();
$model_2->FKAttribute=$model_1->id;
$model_2->save();
}
Regards.