Aliaspooryorik
ColdFusion ORM Book

ValidateThis SOTR2011 presentation code part 2

Part 2 of my code samples from Scotch on the Rocks. Part one is here.

Extending

ValidateThis is written in an object orientated style and allows for certain objects to extended. This is a very powerful feature of the framework. Taking the above example (which is kinda messy), I can extend the BOValidator and the Result objects to include my own methods.

Now before you ask why I'm including HTML markup in my model, don't worry, this is just an example of what you could do!

First off create you own classes. Here is my custom BOValidator.

component extends="ValidateThis.core.BOValidator" 
{
  /**
   * I check if the field is required (for the context if passed), if it is
   * then I return HTML to keep the view nice and clean
   */
  string function renderFieldIsRequired( required string fieldname, string context='' )
  {
    var result = "";
    
    if ( fieldIsRequired( arguments.fieldname, arguments.context ) )
    {
      result = '<em>*</em>';
    }
    return result;
  }
}

Here is my custom Result.

/**
 * I am a specialised version of the default ValidateThis result object
 */
component extends="ValidateThis.util.Result" 
{
  /**
   * I check if the field has an error and if it does then I return
   * the specific error as HTML to keep the view nice and clean
   */
  string function renderErrorByField( required string fieldname )
  {
    var result = "";
    var failureCollection = getFailureMessagesByField();
    var error = "";
    if ( StructKeyExists( failureCollection, arguments.fieldname ) )
    {
      for ( error in failureCollection[ arguments.fieldname ] )
      {
        result &= '<br />' & error;
      }
    }
    return result;
  }
}

Now I can clean up my view my using these new methods. Note, that my ValidateThis config sets the dotted path to by custom objects.

<cfscript>
// configure & load ValidateThis
config = {
  // tell ValidateThis to use the custom Result object
  ResultPath="CustomValidationResult",
  // tell ValidateThis to use the custom BOValidator
  BOValidatorPath="CustomBOValidator"
};
ValidateThis = new validateThis.ValidateThis( config );

User = new User();

if ( StructCount( form ) )
{
  User.setUserName( form.username );
  User.setPassword( form.password );
  // validate the User for the login context
  ValidationResult = ValidateThis.validate( theObject=User, context="login" );
  
  if ( !ValidationResult.hasErrors() )
  {
    WriteOutput( "Welcome " & User.getUsername() );
    abort;
  }
}
else
{
  ValidationResult = ValidateThis.newResult();
}

// get the BO Validator for the User object
BOValidator = ValidateThis.getValidator( theObject=User, context="login" ); 

</cfscript>

<cfoutput>
<p>Login to the Scotch on the Rocks chat room...</p>
<form action="#CGI.SCRIPT_NAME#" method="post" id="validateme">

  <dl>
    <dt><label for="username">Username #BOValidator.renderFieldIsRequired( "username" )#</label></dt>
    <dd>

      <input type="text" name="username" id="username" value="#HtmlEditFormat( User.getusername() )#" />
      #ValidationResult.renderErrorByField( "username" )#
    </dd>

    
    <dt><label for="password">Password #BOValidator.renderFieldIsRequired( "password" )#</label></dt>
    <dd>
      <input type="text" name="password" id="password" value="#HtmlEditFormat( User.getpassword() )#" />

      #ValidationResult.renderErrorByField( "password" )#
    </dd>

    <dt><label for="remember">Remember Me? #BOValidator.renderFieldIsRequired( "remember" )#</label></dt>

    <dd>
      <input type="checkbox" name="remember" id="remember" value="1" />

      #ValidationResult.renderErrorByField( "remember" )#
    </dd>
  </dl>
  <input type="submit" name="submit" id="submit" value="submit" />

</form>

<!--- not using client side validation in this demo to show failureMessages --->
</cfoutput>

Even more...

Hopefully this presentation has given you a good start on getting up an running with ValidateThis, when I presented, then I did demo each script and explain in more details. However, I've only covered a small part of what you can do...

  • Injecting into BO
  • Debugging
  • Even more stuff…
    • i18n support
    • Add rules dynamically (addRule)
    • integration with:
      • ColdBox via interceptor or plugin
      • ModelGlue Validation Service, impliments ModelGlue.util.ValidationErrorCollection
      • CFWheels
      • cfUniForm
    • Validate JSON

Where next?

You can download ValidateThis from http://validatethis.riaforge.org/. Read the docs at: http://www.validatethis.org. Ask questions at: http://groups.google.com/group/validatethis. Fork and contribute at: https://github.com/ValidateThis/ValidateThis.


No comments

Leave a comment

If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

Please note: If you haven't commented before, then your comments will be moderated before they are displayed.

Please subscribe me to any further comments
 

Search

Wish List

Found something helpful & want to say ’thanks‘? Then visit my Amazon Wish List :)

Categories

Recent Posts