Answers.Body: <p>Close!  You want to redirect on success and let the request continue as normal on validation failure.</p>
  
  <pre><code>public function actionMyPluginAction
  {
      $post = craft()-&gt;request-&gt;getPost();
      $requestModel = Micros_AuthModel::populateModel($post);
  
      if ($requestModel-&gt;validate())
      {
          // Yay! What I did works!
  
          $this-&gt;redirectToPostedUrl();
      }
  
      craft()-&gt;urlManager-&gt;setRouteVariables(array(
          'requestModel' =&gt; $requestModel
      ));
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-10T06:56:45.680Z
Answers.Text: OK, so when I redirectToPostedUrl() how can I get the data that was posted originally (to repopulate the form) and display the errors in the model?
Answers.UserDisplayName: Russ Back
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-10T07:22:26.827Z
Answers.Text: Ah got it: craft.request.getPost().
Answers.UserDisplayName: Russ Back
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-09T23:17:01.783Z
Answers.LastActivityDate: 2015-04-09T23:17:01.783Z
Answers.UserDisplayName: Brad Bell
Body: <p>I think I might be confused by <a href="https://craftcms.com/docs/plugins/controllers#posting-to-controller-actions" rel="nofollow noreferrer">https://craftcms.com/docs/plugins/controllers#posting-to-controller-actions</a></p>
  
  <p>I have a controller that I want to return a model when validation fails:</p>
  
  <pre><code>$post = craft()-&gt;request-&gt;getPost();
  $requestModel = Micros_AuthModel::populateModel($post);
  
  if ( ! $requestModel-&gt;validate())
  {
      craft()-&gt;urlManager-&gt;setRouteVariables(array(
          'requestModel' =&gt; $requestModel
      ));
  
      $this-&gt;redirectToPostedUrl();
  }
  </code></pre>
  
  <p>I assumed I would have access to requestModel in the template that posted:</p>
  
  <pre><code>{% if requestModel is defined %}
      {{ inspect(requestModel) }}
  {% endif %}
  </code></pre>
  
  <p>But I that inspect() call never fires.</p>
  
  <p>What am I missing?</p>
  
Tags: <plugin-development><templating><variable>
Title: Can't seem to get variables set in controllers
ThreadText: # Can't seem to get variables set in controllers
  
  (Asked by: username_0 on 2015-04-09)
  
  I think I might be confused by <https://craftcms.com/docs/plugins/controllers#posting-to-controller-actions>
  
  I have a controller that I want to return a model when validation fails:
  
  ```
  $post = craft()->request->getPost();
  $requestModel = Micros_AuthModel::populateModel($post);
  
  if ( ! $requestModel->validate())
  {
      craft()->urlManager->setRouteVariables(array(
          'requestModel' => $requestModel
      ));
  
      $this->redirectToPostedUrl();
  }
  
  ```
  
  I assumed I would have access to requestModel in the template that posted:
  
  ```
  {% if requestModel is defined %}
      {{ inspect(requestModel) }}
  {% endif %}
  
  ```
  
  But I that inspect() call never fires.
  
  What am I missing?
  
  **Tags:** plugin-development, templating, variable
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-04-09. Score: 1 | Accepted answer
  
  Close! You want to redirect on success and let the request continue as normal on validation failure.
  
  ```
  public function actionMyPluginAction
  {
      $post = craft()->request->getPost();
      $requestModel = Micros_AuthModel::populateModel($post);
  
      if ($requestModel->validate())
      {
          // Yay! What I did works!
  
          $this->redirectToPostedUrl();
      }
  
      craft()->urlManager->setRouteVariables(array(
          'requestModel' => $requestModel
      ));
  }
  
  ```
  
  > **Comments:**
  > **username_0**: OK, so when I redirectToPostedUrl() how can I get the data that was posted originally (to repopulate the form) and display the errors in the model?  
  > **username_0**: Ah got it: craft.request.getPost().  
  
  ---
  
  