Home > ColdFusion, Web Application Development > Learning CFWheels – form handling and data validation

Learning CFWheels – form handling and data validation

I have continued with the CFWheels screencast tutorials in building forms and handling form data.  The focus today was on form validation.  CFWheels has some really cool methods to handle the data after form submission.  Using the validatesPresenceOf() in a model CFC, we can ensure required fields have been entered.  Simply pass the required fields to the method and if there are errors, the hasErrors() method with evaluate to true and the save() method will not insert data to the DB.  The just execute the render() method to get back to the form.  Add a call to the errorMessagesFor() to the form view and a nice bulleted list of errors is displayed along with the form.  The form fields with errors is now wrapped with <span class=”field-with-errors”> allowing us to use some CSS to make the errors stand out.  All done with very minimal code.  Coding this took maybe 10 minutes and about 30 total lines of code.  In spaghetti code this would take me at least 30 minutes and would have been considerably more lines of code.

It’s late now, but what comes to mind is overriding the error messages that are generated automagically with messages that mean more to the user.  So instead of saying, “ysnSubscribe is required,” the error message reads “You must choose one of the subscribe options.”  I’m sure there’s a relatively simple way to do this but it’s late so it will have to wait until tomorrow.  I will say, however that I am pleased so far  with how simple some of the more routine coding tasks become with CFWheels.

  1. July 22, 2010 at 7:02 am

    Hey, that’s what Wheels is for is to streamline your routine coding tasks. Glad that you’re enjoying it so far!

    As a hint for custom error messages, take a look at any of the validation methods and notice the message argument.

    • kwbarrett
      July 22, 2010 at 9:15 am

      Thanks Chris. I figured that would be the case. It was very late last night when I was going through this and I planned to look at it today. For me, it cannot be overstated how much I appreciate the thorough documentation provided with CFWheels. Thanks again.

  2. ComboFusion
    August 22, 2010 at 11:50 pm

    Hi there. I was goggleing to find some material about saving data from forms to multiple DB tables and I came across your blog. I will quote a sentence that I recently came across here: http://code.google.com/p/cfwheels/wiki/NestedProperties.

    “When you’re starting out as a Wheels developer, you are probably amazed at the simplicity of a model’s CRUD methods. But then it all gets quite a bit more complex when you need to update records in multiple database tables in a single transaction.”

    Unfortunatelly as is I realized, the NestedProperties() function will be available in Wheels 1.1.

    I also studied in detail Chris’ screencasts. I am sure you will be interested to read more about this topic here: http://groups.google.com/group/cfwheels/browse_thread/thread/7ae1d71787c6de6d.

  1. January 2, 2011 at 2:43 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: