Mark Drew (Redux)- cf_etc...

a compendium of railo, cfml, cfeclipse and technology topics

Mark Drew (Redux)- cf_etc...

Introducing EventValidation: Form and Event Validation

November 25, 2007 ·

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

Tags: coldfusion · coldspring · EventValidation

11 responses

  • 1 Shad Belcher // Sep 22, 2008 at 4:13 PM

    Thanks Mark. You just made my life easier.
  • 2 Cutter // Sep 22, 2008 at 4:13 PM

    Once again, very nice work Brother.
  • 3 Mark Drew // Sep 22, 2008 at 4:13 PM

    Glad to help! I shall document more about the features of EValidation since it can do some funky stuff.
  • 4 shimju david // Sep 22, 2008 at 4:14 PM

    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.
  • 5 Mark Drew // Sep 22, 2008 at 4:14 PM

    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.
  • 6 shimju david // Sep 22, 2008 at 4:14 PM

    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.

    &lt;cfif NOT getModelGlue().getBeanFactory().beanDefinitionExists(validatorBean)&gt;
             &lt;cfset event.trace(&quot;EventValidatation&quot;, &quot;The EventValidation bean #validatorBean# hasn't been defined in the BeanFactory (ColdSpring to you and me)&quot;)&gt;
             &lt;cfreturn /&gt;
          &lt;/cfif&gt;
  • 7 Mark Drew // Sep 22, 2008 at 4:14 PM

    Its as it says on the tin, getBeanFactory() from getModelGlue() is not in MG3 yet, I have talked to Joe about this.
  • 8 Roy Martin // Sep 22, 2008 at 4:14 PM

    In MG3 it's called getInternalBeanFactory, I just change it to the default method and wrapped it in try catch for backwards compatibility.

    &lt;cfset var beanFactor = '' /&gt;
           &lt;!--- Try to use the MG3 BeanFactor otherwise use the MG2 method ---&gt;
    &lt;cftry&gt;
    &lt;cfset beanFactory = getModelGlue().getInternalBeanFactory() /&gt;
    &lt;cfcatch type=&quot;any&quot;&gt;
    &lt;cfset beanFactory = getModelGlue().getBeanFactory() /&gt;
    &lt;/cfcatch&gt;
    &lt;/cftry&gt;
    &lt;!--- Check the bean exists ---&gt;
           &lt;cfif NOT beanFactory.beanDefinitionExists(&quot;EventGuard&quot;)&gt;
  • 9 Roy Martin // Sep 22, 2008 at 4:14 PM

    small typo in the copy/paste, first line is missing a y for the var.
    &lt;cfset var beanFactory = '' /&gt;
  • 10 wilson Imarhiagbe // Nov 27, 2008 at 4:23 PM

    need eventvalidation method register
  • 11 yqxfoaqtdb // Jul 25, 2011 at 11:04 AM

    yzYwEW <a href="http://yrwrsqyhvbhd.com/">yrwrsqyhvbhd</a>;, [url=http://yqnsvgxabvvn.com/]yqnsvgxabvvn[/url], [link=http://nhghvqdicgiq.com/]nhghvqdicgiq[/link], http://owlvpfeavdna.com/