Answers.Body: <p>You can call <code>setRouteVariables</code> and not return anything from your controller so the request continues as normal.  Craft does this all the time with it's own controllers.  See <a href="https://craftcms.stackexchange.com/a/12033/57">this answer</a> for the general controller workflow.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-30T18:24:51.543Z
Answers.Text: This is definitely the better way to handle it. I saw how Craft was doing it, but I didn't understand that returning nothing without redirecting would reload the original page. Thank you for this information!
Answers.UserDisplayName: John O
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-30T16:45:14.727Z
Answers.LastActivityDate: 2015-10-30T16:45:14.727Z
Answers.LastEditDate: 2017-04-13T12:51:00.303Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <p>Ok, so I realized even though the template is being called via route, it's still a template, duh. So I can use <code>$this-&gt;renderTemplate</code> in the controller and specify the data there. Is there a more elegant way than this though?</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-30T15:02:37.490Z
Answers.LastActivityDate: 2015-10-30T15:02:37.490Z
Answers.UserDisplayName: John O
Body: <p>I currently have a form that is being displayed to the user via a custom route.</p>
  
  <p>The form submits to a controller. The first thing the controller does is populate a custom model and validates it.</p>
  
  <p>If the model fails validation, I want to skip the rest of the controller and pass the model back to the form. What is the best way to do this? I tried using <code>craft()-&gt;urlManager-&gt;setRouteVariables</code> and a <code>$this-&gt;redirect</code>, but the route variable didn't seem to come through, which I imagine is because <code>setRouteVariables</code> isn't supposed to be used like that.</p>
  
Tags: <routes><controller><validation>
Title: Passing data back to form on invalid submission
ThreadText: # Passing data back to form on invalid submission
  
  (Asked by: username_0 on 2015-10-30)
  
  I currently have a form that is being displayed to the user via a custom route.
  
  The form submits to a controller. The first thing the controller does is populate a custom model and validates it.
  
  If the model fails validation, I want to skip the rest of the controller and pass the model back to the form. What is the best way to do this? I tried using `craft()->urlManager->setRouteVariables` and a `$this->redirect`, but the route variable didn't seem to come through, which I imagine is because `setRouteVariables` isn't supposed to be used like that.
  
  **Tags:** routes, controller, validation
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-10-30. Score: 2 | Accepted answer
  
  You can call `setRouteVariables` and not return anything from your controller so the request continues as normal. Craft does this all the time with it's own controllers. See [this answer](https://craftcms.stackexchange.com/a/12033/57) for the general controller workflow.
  
  > **Comments:**
  > **username_0**: This is definitely the better way to handle it. I saw how Craft was doing it, but I didn't understand that returning nothing without redirecting would reload the original page. Thank you for this information!  
  
  ---
  
  ### Answer by username_0 on 2015-10-30. Score: 1
  
  Ok, so I realized even though the template is being called via route, it's still a template, duh. So I can use `$this->renderTemplate` in the controller and specify the data there. Is there a more elegant way than this though?
  
  
  ---
  
  