Answers.Body: <p>In Yii2 there is no such a thing as "redirectWithErrors". Usually you have 2 scenarios</p>
  
  <ol>
  <li>There are validation errors -> display the form again with those errors</li>
  <li>Successful -> redirect</li>
  </ol>
  
  <p>The first one doesn't require a redirect. You just return <code>null</code> in your controller and Yii2 will route the to the url specified via <code>GET</code>parameter.</p>
  
  <p>So you can have a form like this. </p>
  
  <pre><code>&lt;form method="post" action="myUrl"&gt;
      &lt;input type="hidden" name="action" value="mycontroller/save"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Yii2 will do the following</p>
  
  <ol>
  <li>Route to your controller and hit the <code>save</code> action</li>
  <li>When you redirect or return a valid object of type <code>yii\web\Response</code> or a string that can be converted into that it will return the <code>Response</code></li>
  <li>When you don't return something it will route the request a second time with your get param <code>myUrl</code>, that can be treated as you <code>routeWithErrors</code> function because you set the errors <code>Craft::$app-&gt;getUrlManager()-&gt;setRouteParams(['model' =&gt; $model])</code> and have then access to these parameters.</li>
  </ol>
  
  <p>If you really dislike this, you could use <code>return Craft::runAction('controller/action', $parameters);</code> to run a controller action with certain values</p>
  
  <p><strong>Edit</strong> 
  Usually you want to leave <code>action="myUrl"</code> blank and don't insert it. That way Yii2 will automatically reload the current route so you can dispaly your errors</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-08T11:41:27.113Z
Answers.LastActivityDate: 2019-04-08T14:59:53.117Z
Answers.LastEditDate: 2019-04-08T14:59:53.117Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I cant get this to work.</p>
  
  <p>I'm doing a post to a controller action. From that action I'm doing a lot of validations, db insert, file uploads etc.</p>
  
  <p>Coming from Laravel Im used to is doing like this: (<em>example with no real variables or methods. Just for visual presentation</em>)</p>
  
  <pre><code>if(!$something-&gt;success){
    return redirect("controller-url")-&gt;withErrors([$model-&gt;errors]);
  }
  // Next steps...
  </code></pre>
  
  <p>From what I can read in the docs Craft recommend posting to the same page to handle errors and pass the model down to the view, but then doing a page refresh will do the same again so I dont like that.</p>
  
  <p>My Controller is like this : </p>
  
  <p><strong>Index</strong> (Renders index template)</p>
  
  <pre><code>public function actionIndex()
  {
      $this-&gt;renderTemplate("plugin/settings");
  }
  </code></pre>
  
  <p><strong>Post</strong></p>
  
  <pre><code>public function actionPost()
  {
    if(!$model-&gt;validate()){
      Craft::$app-&gt;getSession()-&gt;setError($message);
      return $this-&gt;redirect("url-to-index-action");
    }
  }
  </code></pre>
  
  <p>So the setError() works fine but I want to the pass all the errors from the model to the index action after a redirect and then down to the twig template. How?</p>
  
Tags: <plugin-development><controller>
Title: Craft 3 Controller - Redirect with errors
ThreadText: # Craft 3 Controller - Redirect with errors
  
  (Asked by: username_0 on 2019-04-08)
  
  I cant get this to work.
  
  I'm doing a post to a controller action. From that action I'm doing a lot of validations, db insert, file uploads etc.
  
  Coming from Laravel Im used to is doing like this: (*example with no real variables or methods. Just for visual presentation*)
  
  ```
  if(!$something->success){
    return redirect("controller-url")->withErrors([$model->errors]);
  }
  // Next steps...
  
  ```
  
  From what I can read in the docs Craft recommend posting to the same page to handle errors and pass the model down to the view, but then doing a page refresh will do the same again so I dont like that.
  
  My Controller is like this :
  
  **Index** (Renders index template)
  
  ```
  public function actionIndex()
  {
      $this->renderTemplate("plugin/settings");
  }
  
  ```
  
  **Post**
  
  ```
  public function actionPost()
  {
    if(!$model->validate()){
      Craft::$app->getSession()->setError($message);
      return $this->redirect("url-to-index-action");
    }
  }
  
  ```
  
  So the setError() works fine but I want to the pass all the errors from the model to the index action after a redirect and then down to the twig template. How?
  
  **Tags:** plugin-development, controller
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-04-08. Score: 2 | Accepted answer
  
  In Yii2 there is no such a thing as "redirectWithErrors". Usually you have 2 scenarios
  
  1. There are validation errors -> display the form again with those errors
  2. Successful -> redirect
  
  The first one doesn't require a redirect. You just return `null` in your controller and Yii2 will route the to the url specified via `GET`parameter.
  
  So you can have a form like this.
  
  ```
  <form method="post" action="myUrl">
      <input type="hidden" name="action" value="mycontroller/save">
  </form>
  
  ```
  
  Yii2 will do the following
  
  1. Route to your controller and hit the `save` action
  2. When you redirect or return a valid object of type `yii\web\Response` or a string that can be converted into that it will return the `Response`
  3. When you don't return something it will route the request a second time with your get param `myUrl`, that can be treated as you `routeWithErrors` function because you set the errors `Craft::$app->getUrlManager()->setRouteParams(['model' => $model])` and have then access to these parameters.
  
  If you really dislike this, you could use `return Craft::runAction('controller/action', $parameters);` to run a controller action with certain values
  
  **Edit**
  Usually you want to leave `action="myUrl"` blank and don't insert it. That way Yii2 will automatically reload the current route so you can dispaly your errors
  
  
  ---
  
  