Note that there are some explanatory texts on larger screens.

plurals
  1. POYii redirect to same view after multiple model validation and save
    primarykey
    data
    text
    <p>Based on <a href="http://www.yiiframework.com/doc/guide/1.1/en/form.table" rel="nofollow">this article</a>, I have a similar solution. This is the example:</p> <pre><code>public function actionBatchUpdate() { // retrieve items to be updated in a batch mode // assuming each item is of model class 'Item' $items=$this-&gt;getItemsToUpdate(); if(isset($_POST['Item'])) { $valid=true; foreach($items as $i=&gt;$item) { if(isset($_POST['Item'][$i])) $item-&gt;attributes=$_POST['Item'][$i]; $valid=$item-&gt;validate() &amp;&amp; $valid; } if($valid) // all items are valid // ...do something here } // displays the view to collect tabular input $this-&gt;render('batchUpdate',array('items'=&gt;$items)); } </code></pre> <p>The problem is, I don't know what is going on at the end of the process. if I don't put any redirect here:</p> <pre><code>if ($valid) { foreach ($models as $model) { $model-&gt;save(); } } </code></pre> <p>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:</p> <pre><code>if ($valid) { foreach ($models as $model) { $model-&gt;save(); } $this-&gt;redirect... } </code></pre> <p>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?</p> <p>and if I put redirect here:</p> <pre><code>if ($valid) { foreach ($models as $model) { $model-&gt;save(); } } $this-&gt;redirect... </code></pre> <p>models' save and redirect goes fine, but I never get validation errors.</p> <p>I have tried to play with unsetting $_POST, but I have no clue what is missing. what am I missing?</p> <p>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:</p> <pre><code>'checked' =&gt; '$_POST["rendelesGyartmanyId"][$row]', </code></pre> <p>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.</p> <p>UPDATE_2: not sure, but maybe the problem is with the browser keeping selected values...? I've found <a href="http://www.yiiframework.com/forum/index.php/topic/16219-redirectrender-the-create-page-again-after-form-submit/" rel="nofollow">this thread</a> about a similar problem.</p> <p>the problem is, 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.</p> <p>UPDATE 3: very interesting. if I comment out model save here:</p> <pre><code>if ($valid) { foreach ($models as $model) { // $model-&gt;save(); } $this-&gt;redirect... } </code></pre> <p>redirect is recognized and working, but as soon as I remove commenting out, redirect is not recognized. I've put a false view command there and this way I could see that Yii doesn't recognize it.</p> <p>Can somebody please make it clear for me what is going on here?</p> <p>thanks a lot!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload