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.


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 for the general controller workflow.

  • 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
    Oct 30 '15 at 18:24

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.