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.