<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>

CC BY-SA 3.0
2015-10-30T18:24:51.543Z
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!
John O
CC BY-SA 3.0
2015-10-30T16:45:14.727Z
2015-10-30T16:45:14.727Z
2017-04-13T12:51:00.303Z
Brad Bell
<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>

CC BY-SA 3.0
2015-10-30T15:02:37.490Z
2015-10-30T15:02:37.490Z
John O
<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>

<routes><controller><validation>
Passing data back to form on invalid submission
# 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?


---

