model-view-controllercoldfusionrailocfwheelsbluedragon

CFWheels - How can I use the errorMessagesFor after a redirect?


Is there any way to use the #errorMessagesFor()# method after a redirect in the controller?

Reason I ask is that I want the form to stay on the same page.

I have a URL like:

/link/submit/

With a form that posts to:

/link/linksubmit/

But I want it recirected back to /link/submit/.

I can do this, and it works, but then the #errorMessagesFor()# no longer works in my view.

I don't want to use a "flash" message for this because I want specific error messages to be shown for the fields on the form. Is there perhaps a way to pass the validation errors to a flash message?

Thanks, Michael.


Solution

  • Wheels isn't really designed for the pattern that you're suggesting. What you need to do is render the form page in your linksubmit action after validation fails.

    This is the pattern to follow:

    <cffunction name="linksubmit">
        <cfscript>
            link = model("link").new(params.link);
    
            if (link.save()) {
                redirectTo(route="someRoute", success="The link was created successfully.");
            }
            else {
                flashInsert(error="There was an error saving the link. Please review the errors below and try again.");
                renderPage(action="submit");
            }
        </cfscript>
    </cffunction>