ColdFusion ColdBox and form validation

Today I want to quickly talk about doing form validation in ColdBox, this isn't really anything new to ColdBox but it isn't really documented at the moment either. For those who don't know or are not aware of it, ColdBox actually has HyRule as part of its tools line up that is built right into ColdBox.

So how easy is this validation, pure and simple it couldn't get any easier.

I am going to be using ColdFusion 9 as my code will show, but with a small bit of modification this can work with ColdFusion 8 just as easy.

To begin I have now began getting into the habit of using beans more and more in my code, especially when designing new applications as the convenience is really handy. With that out of the way lets create a very simple handler in our application, also be aware that from time to time I may mention controller. I prefer this term, more than a handler but in case I do slip up and use it then you'll know what I am referring to.

The code for the controller is:

component extends="coldbox.system.EventHandler" {
}

Right now this is doing nothing special, so lets add an action for our handler to do a login.

void function doLogin(event) {
   var rc = event.getCollection();
}

Let's now create the bean.

component accessors="true" {
   /** Used in user-user communications.
      Hyrule Data Validation Rules
       @Min 2
       @Max 25
       @String */
   property name="username" type="string";
   /** Used to capture the password from the User.
       Hyrule Rules
      @Password 6,20,medium */
   property name="password" type="string";
}

If you are familiar with ColdFusion 9 then this will look very familiar to you, if not I'll explain what is going on. In ColdFusion 9 we can now have a component and if we apply the attribute accessors as true, then we are saying to ColdFusion to automatically give us setters/getters to our component.

The other thing you will notice is the comments that are there for Hyrule, these are the definitions that we are setting up for each property. And as you can see we are saying that the username is going to be a string, and the minimum number for the length is 2 and max is 25.

Very basic definition for this example.

So with that in mind I'll also point out that this can also apply to our ORM entities as well, and with a username and password it would be best to actually define this information in the Entity rather than creating a bean to do this for us.

Last but not least, we now need to make some code changes to our controller. And the first thing is we are going to do some DI (Dependency  Injection) to make our life so much easier, and with WireBox in ColdBox this is extremely fun and easy.

component extends="coldbox.system.EventHandler" {
property name='BeanFactory' inject='coldbox:plugin:BeanFactory';
property name="ValidationService" inject='model';
property name="loginFormBean" inject='model';
}

As you can see we are injecting 3 items into our handler, the BeanFactory and 2 custom ones we are creating. One we created earlier which was the loginFormBean, and as you can see we have saved this in our model directory. The second one is our ValidationService, which we will create in a minute and is also stored in our model directory.

component singleton extends="coldbox.system.orm.hibernate.hyrule.Validator" {
}

In this example I am not going to do anything else here, but normally I would have some more code. I should also note to you, is that even though it is empty we could have easily have inject the extends object instead into the controller.

So that's pretty much the very basics, to get it up and ready to be used in our action handler. You will also notice that I have also defined this as a singleton as well, and won't go into the details on how that works.

Now for the validation check, we'll add the following code to our handler.

void function doLogin(event) {
   var rc = event.getCollection();
   rc.loginInfo = BeanFactory.populateBean(target=loginFormBean);
   writeDump(ValidationService.validate(rc.loginInfo)); abort;
}

So what are we doing here is actually very simple, and very easy to do and very little code as well. The first thing we do is we populate the bean loginFormBean, with what data that is coming in from the form. The beauty here is that you don't need to know what to pass and what not to pass, it is just done automatically for you. There is more to the population in which I won't go into here, but you could easily tell what to include and exclude as well.

The other line inside the dump is the actual call to the validationService, which will return an object if there are errors. And that is pretty much it, to test this we can then just do the following in our browser to test this. The first will pass, while the second should fail.

http://localhost/general/dologin/username/testuser/password/testpassword
http://localhost/general/dologin/username/testuser/password

As you can see we are using SES here with rewrite, but you can also do the following.

http://localhost/index.cfm/general/dologin/username/testuser/password/testpassword
http://localhost/index.cfm/general/dologin/username/testuser/password

or

http://localhost/index.cfm?general.dologin&username=testuser&password=testpassword
http://localhost/index.cfm?general.dologin&username=testuser&password

That's pretty much it for this example, but from here you could easily put the results into the flash and then use the view to display the output of the errors.


@GregOstravich Use this and let me know, you might need to do apply the ORMService.save(entity=entityName) http://bit.ly/cz9gQF Sep 9, 2010
ColdFusion ColdBox and form validation - http://cfbloggers.org/?c=44116 Aug 30, 2010
Doing form validation in ColdBox - http://bit.ly/cz9gQF #ColdFusion Aug 30, 2010