"Introducing EventValidation: Form and Event Validation"
EventValidation is another Action Pack for Model-Glue designed towards the validation of forms, but can be used to validate any value within a Model-Glue event.
It provides a way to tag up your form and for it to be validated, both client side and server side, which is something that I find very neat, since I hate repeating CF code in the backend with JavaScript code in the front end.
Let me walk you through a simple installation and setup of EventValidation.
INSTALLATION
Simply extract the EventValidation Action Pack to a secure part of your webserver and create a ColdFusion mapping to "/EventValidation". Then in your model glue file add the following at the top before any other controllers etc:
< include template="/EventValidation/config/EventValidation.xml"/>
You will be defining the rules to validate an event against in EventValidation/config/ColdSpring.xml, so import it into your application's ColdSpring.xml, using a relative path to the resource (I think there is a bug here with ColdSpring but I need to clarify this with Chris Scott) :
The next thing to do, is in the form that you want to validate, is to add an cfimport declaration at the top:
This will allow you to setup your form with a custom tag as follows:
__MSP_CODE_BLOCK_1__
What is happening here? well, using ev:setup we have said that the id if the bean that will do our validation is called ev_Register, and that the success event will be register.action, this form is a normal form, with only the ev:setup added.
Now, lets define what we need in this form to be validated, in the EventValidation/config/ColdSpring.xml I have the following bean:
What will happen now when you submit your form, is that an ErrorCollection object will be added to the Event. If there are any errors in the collection, it will return to the event that the form is in and you then do ViewState.getValue("errorCollection").getErrorCollection() to get an array of errors.
So that is a simple way to get started with validation, but this is not the end of what EventValidator can do for you, in the next post I shall examine how to add different styles to the error fields and how to add error descriptions to each element (it does it automagically!)
UPDATE: you can see a simple demo here