1

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?

1

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
    ));
}
  • 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? – Russ Back Apr 10 '15 at 6:56
  • Ah got it: craft.request.getPost(). – Russ Back Apr 10 '15 at 7:22

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.