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:
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:
<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
Now, lets define what we need in this form to be validated, in the EventValidation/config/ColdSpring.xml I have the following bean:
<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>
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



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