Yii redirect to same view after multiple model validation and save -


based on this article, have similar solution. example:

public function actionbatchupdate() {     // retrieve items updated in batch mode     // assuming each item of model class 'item'     $items=$this->getitemstoupdate();     if(isset($_post['item'])) {         $valid=true;         foreach($items $i=>$item) {             if(isset($_post['item'][$i]))                 $item->attributes=$_post['item'][$i];             $valid=$item->validate() && $valid;         }         if($valid)  // items valid             // ...do here     }     // displays view collect tabular input     $this->render('batchupdate',array('items'=>$items)); } 

the problem is, don't know going on @ end of process. if don't put redirect here:

if ($valid) {     foreach ($models $model) {         $model->save();     } } 

models saved fine, validation errors fine, form stays filled. it's not me. if put redirect here:

if ($valid) {     foreach ($models $model) {         $model->save();     }     $this->redirect... } 

models saved fine, validation errors unique rule these records exist. seems there model save second time or what?

and if put redirect here:

if ($valid) {     foreach ($models $model) {         $model->save();     } } $this->redirect... 

models' save , redirect goes fine, never validation errors.

i have tried play unsetting $_post, have no clue missing. missing?

update: maybe it's important, redirect same page rendered. i'm nost sure need redirect anyway. interesting thing is, unique validation error when in grid checkboxcolumn:

'checked' => '$_post["rendelesgyartmanyid"][$row]', 

i this, make sure when user checks lot of checkboxes, , there validation errors, , page reloaded, it's not necessary recheck checkboxes, maybe it's not solution.

update_2: not sure, maybe problem browser keeping selected values...? i've found this thread similar problem.

the problem is, after submit, , redirect same view, post still set, , values still in there. that's why yii wants submit again, have unique rule defined, validation error. common behaviour of yii, after dealing post data has redirected somewhere else? there no other way somehow redirect same page clean start? i've tried unset models' attributes, , post, couldn't solve it, maybe not doing way.

update 3: interesting. if comment out model save here:

if ($valid) {     foreach ($models $model) {         // $model->save();     }     $this->redirect... } 

redirect recognized , working, remove commenting out, redirect not recognized. i've put false view command there , way see yii doesn't recognize it.

can please make clear me going on here?

thanks lot!

did use ajax validation form? if yes, implement code ajax validate on controller or set ajax validation false. code below right:

if ($valid) {    foreach ($models $model) {        $model->save(false); //the model has been validated, no need validate again    }    $this->redirect... } 

Comments