Introducing EventValidation: Form and Event Validation

Posted At : November 25, 2007 9:32 AM | Posted By : Mark Drew
Related Categories: coldfusion, model-glue, coldspring, EventValidation

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) :

<import resource="../../EventValidation/config/ColdSpring.xml" />

The next thing to do, is in the form that you want to validate, is to add an cfimport declaration at the top:

<cfimport prefix="ev" taglib="/EventValidation/taglib">

This will allow you to setup your form with a custom tag as follows:

<h1>Register</h1>
   <cfimport prefix="ev" taglib="/EventValidation/taglib">
   <cfoutput>   
   <form action="#ViewState.getValue("myself")#register.action" method="post" >
      <ev:setup id="ev_Register" successEvent="register.action">
      <div>
       <label for="email">email:</label>
         <input type="text" name="email" id="email" value="#ViewState.getValue("email")#">
      
       <label for="password">password:</label>
         <input type="password" name="password" id="password" value="#ViewState.getValue("password")#">
      </div>
      <div>
         <input type="submit">
      </div>
   </form>

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 added.

Now, lets define what we need in this form to be validated, in the EventValidation/config/ColdSpring.xml I have the following bean:

<bean id="ev_Register" class="EventValidation.model.EventValidator">
       <constructor-arg name="rules">
          <list>
             <map>
                <entry key="field"><value>email</value></entry>
                <entry key="rule"><value>required</value></entry>
             </map>
             <map>
                <entry key="field"><value>email</value></entry>
                <entry key="rule"><value>email</value></entry>
             </map>
             
             <map>
                <entry key="field"><value>password</value></entry>
                <entry key="rule"><value>required</value></entry>
             </map>
          </list>
       </constructor-arg>
    </bean>
The id matches the id in our ev:setup tag, and then we have a list of maps, or in CF it would be an array of structs, so the first entry says, that the field email is required, then we say that email should match the "email" rule, which is using ColdFusion's internal isValid() list of attributes. Then we do the same thing for the password.

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

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Shad Belcher's Gravatar Thanks Mark. You just made my life easier.
# Posted By Shad Belcher | 11/25/07 1:10 PM
Cutter's Gravatar Once again, very nice work Brother.
# Posted By Cutter | 11/26/07 9:48 AM
Mark Drew's Gravatar Glad to help! I shall document more about the features of EValidation since it can do some funky stuff.
# Posted By Mark Drew | 11/26/07 10:09 AM
shimju david's Gravatar HI Mark,

When i tested this form validation actionpack with MG3.O Alpha, it is not working.
Can you please check what went wrong as it is an inevitable tool in our all MG projects.
# Posted By shimju david | 6/9/08 11:37 AM
Mark Drew's Gravatar Well, MG 3 is ALPHA! I wont check what is wrong until MG is at least Beta.
I know for a fact that there are a number of changes and bugs in MG3 (I was doing some coding right next to Joe Rinehart last week)

Besides, why isnt it working? any ideas? any checks etc... etc.?

Its not totally complicated, only two functions. Surely you could find a fix or at least tell me the error and submit a patch. THAT is the point of open source software after all, not just to have developers work for you for free.
# Posted By Mark Drew | 6/9/08 12:13 PM
shimju david's Gravatar The error Iam getting when I submit the form on MG3 applicaton is
The method getBeanFactory was not found in component D:\Inetpub\wwwroot\ModelGlue3\gesture\ModelGlue.cfc.
Detail    Ensure that the method is defined, and that it is spelled correctly.

Then I commented the below line inside controller.cfc of action pack, but now error is not coming but it is not validating the form.

<cfif NOT getModelGlue().getBeanFactory().beanDefinitionExists(validatorBean)>
         <cfset event.trace("EventValidatation", "The EventValidation bean #validatorBean# hasn't been defined in the BeanFactory (ColdSpring to you and me)")>
         <cfreturn />
      </cfif>
# Posted By shimju david | 6/11/08 6:18 AM
Mark Drew's Gravatar Its as it says on the tin, getBeanFactory() from getModelGlue() is not in MG3 yet, I have talked to Joe about this.
# Posted By Mark Drew | 6/11/08 7:45 AM