1

I have a form which I am validating using CFWheels model validation and form helpers.

My code for index() Action/View in controller:

public function index()
{
    title = "Home";
    forms = model("forms");
    allforms = model("forms").findAll(order="id ASC");
}

#startFormTag(controller="form", action="init_form")#
    <select class="form-control">
        <option value="">Please select Form</option>
        <cfloop query="allforms">
            <option value="#allforms.id#">#allforms.name#</option>
        </cfloop> 
    </select>
    <input type="text" name="forms[name]" value="#forms.name#">
    #errorMessageOn(objectName="forms", property="name")#
    <button type="submit">Submit</button>
#endFormTag()#

This form is submitted to init_form() action and the code is :

public function init_form()
{
    title = "Home";
    forms = get_forms(params.forms);

    if(isPost())
    {
        if(forms.hasErrors())
        {
            // don't want to retype allforms here ! but index page needs it
            allforms = model(tables.forms).findAll(order="id ASC");
            renderPage(action="index");
            //redirectTo(action="index");
        } 
    }
}

As you can see from the above code I am validating the value of form field and if any errors it is send to the original index page. My problem is that since I am rendering page, I also have to retype the other variables that page need such as "allforms" in this case for the drop down.

Is there a way not to type such variables? And if instead of renderPage() I use redirectTo(), then the errors don't show? Why is that?

Just to be clear, I want to send/redirect the page to original form and display error messages but I don't want to type other variables that are required to render that page? Is there are way.

Please let me know if you need more clarification.

Saad A
  • 1,135
  • 2
  • 21
  • 46
  • Why don't you flash message functionality of wheels to display error message – Keshav jha Mar 03 '16 at 04:06
  • How do I pass the auto generate errors I have defined in the model then? If I use flash, then I have to write my own validation code and pass my own validation message. I want to use CFWheels functionality to generate server side errors. But then thing is how do I pass the error as a parameter? or even trigger the error on the redirected page. – Saad A Mar 03 '16 at 15:40

1 Answers1

3

This may seem a little off topic, but my guess is that this is an issue with the form being rendered using one controller (new) and processed using another (create) or in the case of updating, render using edit handle form using update.

I would argue, IMHO, etc... that the way that cfWheels routes are done leaves some room for improvement. You see in many of the various framework's routing components you can designate a different controller function for POST than your would use for GET. With cfWheels, all calls are handled based on the url, so a GET and a POST would be handled by the same controller if you use the same url (like when a form action is left blank).

This is the interaction as cfwheels does it:

cfwheels way of doing it

While it is possible to change the way it does it, the documentation and tutorials you'll find seem to prefer this way of doing it.

TL; DR;

The workaround that is available, is to have the form be render (GET:new,edit) and processing (POST:create,update) handled by the same controller function (route). Within the function...

  • check if the user submitted using POST
    • if it is POST, run a private function (i.e. handle_create()) that handles the form
    • within the handle_create() function you can set up all your error checking and create the errors
    • if the function has no errors, create (or update) the model and optionally redirect to a success page
    • otherwise return an object/array of errors
  • make the result error object/array available to view
  • handle the form creation

In the view, if the errors are present, show them in the form or up top somewhere. Make sure that the form action either points to self or is empty. Giving the submit button a name and value can also help in determining whether a form was submitted.

This "pattern" works pretty well without sessions.

Otherwise you can use the Flash, as that is what it was created for, but you do need to have Sessions working. their use is described here: http://docs.cfwheels.org/docs/using-the-flash and here:http://docs.cfwheels.org/v1.4/docs/flashmessages

but it really is as easy as adding this to your controller

    flashInsert(error="This is an error message.");

and this to your view

    <cfif flashKeyExists("error")>
        <p class="errorMessage">
            #flash("error")#
        </p>
    </cfif>
Daniel
  • 34,125
  • 17
  • 102
  • 150