I am a newbie of Yii framework.
I have a form to collect data for two models. The error msgs are perfectly shown, after entering data if i submit the fields are cleared and the same errors again comes.
controller code:
public function actionCreate()
{
$model=new Customer;
$model2=new ShippingAddress;
$this->performAjaxValidation($model);
$this->performAjaxValidationad($model2);
if(isset($_POST['Customer']) && isset($_POST['ShippingAddress']))
{
echo 'hello';
$valid=$model->validate();
$valid=$model2->validate() && $valid;
if($valid)
{
//echo $_POST['Customer']['state_id'];die;
$model->attributes=$_POST['Customer'];
$model2->attributes=$_POST['ShippingAddress'];
}
}
}
The error summary in form
<?php echo $form->errorSummary(array($model,$model2)); ?>
This is the form generating errors, after submitting the form…
4482
Need help guyz…
menxaca
(Menxaca)
July 3, 2013, 8:04am
2
You need to save your models!
public function actionCreate()
{
$model=new Customer;
$model2=new ShippingAddress;
$this->performAjaxValidation($model);
$this->performAjaxValidationad($model2);
if(isset($_POST['Customer']) && isset($_POST['ShippingAddress']))
{
echo 'hello';
$valid=$model->validate();
$valid=$model2->validate() && $valid;
if($valid)
{
//echo $_POST['Customer']['state_id'];die;
$model->attributes=$_POST['Customer'];
$model2->attributes=$_POST['ShippingAddress'];
//Save your models here!
$model->save();
$model2->save();
}
}
}
If these two models have some relation between them, then you will have to assign the foreign key before save, because if not it will fail due to integrity constraint
Hi
may be helpful
if(isset($_POST['Customer']) && isset($_POST['ShippingAddress']))
{
$model->attributes=$_POST['Customer'];
$model2->attributes=$_POST['ShippingAddress'];
if (!$model->hasErrors()) {
Yii::app()->user->setFlash("success", "Thanks for Vendor Regisration.!");
$this->redirect(array('vendor'));
Yii::app()->end();
}
}
boaz
(Boaz Rymland)
July 4, 2013, 4:34am
4
Like already said, you need to save() your models… .
If that still doesn’t work, this wiki article might assist in telling you why.