Based on this article, I have a similar solution. This is the example:
public function actionBatchUpdate() {
// retrieve items to be updated in a batch mode
// assuming each item is of model class 'Item'
$items=$this->getItemsToUpdate();
if(isset($_POST['Item'])) {
$valid=true;
foreach($items as $i=>$item) {
if(isset($_POST['Item'][$i]))
$item->attributes=$_POST['Item'][$i];
$valid=$item->validate() && $valid;
}
if($valid) // all items are valid
// ...do something here
}
// displays the view to collect tabular input
$this->render('batchUpdate',array('items'=>$items));
}
The problem is, I don’t know what is going on at the end of the process: if($valid)…
if I don’t put any redirect here:
if ($valid) {
foreach ($models as $model) {
$model->save();
}
}
models are saved fine, I get validation errors fine, but the form stays filled. It’s not good for me.
if I put redirect here (I think it should be here):
if ($valid) {
foreach ($models as $model) {
$model->save();
}
$this->redirect...
}
models are saved fine, but I get validation errors for my unique rule that these records already exist. it seems like there is model save for a second time or what?
and if I put redirect here:
if ($valid) {
foreach ($models as $model) {
$model->save();
}
}
$this->redirect...
models’ save and redirect goes fine, but I never get validation errors.
I have tried to play with unsetting $_POST, but I have no clue what is missing. what am I missing?
UPDATE: maybe it’s important, that I would like to redirect to the same page what is basically rendered.
I’m nost sure I need redirect anyway.
Interesting thing is, that I get only my unique validation error back when I do this in grid for the checkboxcolumn:
'checked' => '$_POST["rendelesGyartmanyId"][$row]',
I do this, to make sure that when a user checks a lot of checkboxes, and there are any validation errors, and the page is reloaded, it’s not necessary to recheck checkboxes, but maybe it’s not a good solution.
UPDATE_2: the problem is maybe, that after submit, and redirect to same view, POST is still set, and values are still in there. that’s why Yii wants to submit again, but as I have a unique rule defined, I get the validation error. Is it a common behaviour of Yii, that after dealing with POST data it HAS TO be redirected to somewhere else? Is there no other way to somehow redirect to same page as a clean start? I’ve tried to unset models’ attributes, and POST, but I couldn’t solve it, but maybe I was just not doing it the good way.
UPDATE 3: very interesting. if I comment out model save here:
if ($valid) {
foreach ($models as $model) {
// $model->save();
// Static::model()->function();
}
$this->redirect...
}
redirect is recognized and working, but as soon as I remove commenting out, redirect is not recognized. I’ve put a false view there and this way I could see that Yii doesn’t recognize it. What is the connection between code lines in foreach and redirect after foreach.
Can somebody please make it clear for me what is going on here?
thanks a lot!