Validation

D'oh, so we need some validation apparently. Luckily for us, we are learning CFWheels which has built in validation to help us with saves, creates, and updates.
In our model cfc of the contacts table, located at Models/contact.cfc, add this code:

By now you can see CFWheels is pretty intuitive with their naming, so validatesPresenceOf does just that, we could pass in a list of properties but for Contact-O-Matic that isn't needed.
Review the file \wheels\model\validations.cfm in our application for information on the different validation options.
To read more about Object Validation.
In /Controllers/contact.cfc replace our previous create action with this code.

to your URL. This tells CFWheels to reload the entire framework. If you are still not seeing the message, recheck our code.
Some points I'd like to cover, we changed

<cfset model("Contact").create(params.newContact)>

to

<cfset newContact = model("Contact").new(params.newContact)>

. This creates a newContact object based on the contact model. Then we call the save action on it. This will return true for successful or false for unsuccessful. Based on this, we either go to the list action or show the new view which has our form. Notice with

renderPage()

, the process just includes the new view page for new action. If it ran the new action code, we would have a blank form but CFWheels keeps our scope to repopulate the form on

renderPage()

.
One other thing, notice our URL is now at

http://localhost/index.cfm/contact/create

. This is because our form submits to the create action since we did a

renderPage()

on false the page keeps loading and shows our error messages.

Refactor

Now a little refactoring, you may remember we left our index action and view in a mess. Delete the index action in Controllers/contact.cfc and put this code in Views/contact/index.cfm .

<cfoutput>
<h1>Contact-O-Matic</h1>
This application is to manage contacts. It demonstrates how to build an application using <a href="http://www.cfwheels.org">CFWheels</a>
To add a contact #linkTo(text="click here",controller="contact",action="new")#.
To see a list of our contacts, #linkTo(text="click here",controller="contact",action="list")#.
</cfoutput>

And run the page, hopefully you will see the screenshot below our URL choices.

Go ahead, add some contacts to our Contact-O-Matic, make sure our validation is working, and the links. Pat yourself on the back, you have done a good job. Next time in the series, we will cover the ORM in more detail, adding an update and delete page, and contact type table.

Mike Henke (pronounced hang-kee) has worked in ColdFusion development and databases since 1999. I have lived in Washington, DC area. I moved back to Omaha. My main focus currently is learning AngularJS and Ruby on Rails. I am also really interested in learning more about making development easier and more productive.