The ValidateThis Facade Object
The ValidateThis Facade Object is an object that you create in your application code, and which you subsequently use to interact with the framework. It is a cfc named ValidateThis.cfc, which resides in the root of the ValidateThis folder.
Creating the ValidateThis Facade Object The ValidateThis Facade Object is designed to be created as a singleton, meaning there is one instance of it in your application and each request talks to the same instance. For that reason you can create an instance of the object manually, placing it into the application scope, or via an IOC container such as Coldspring.
Creating the Object Manually
To create the object manually, just use
Creating the Object via Coldspring
To create the object via Coldspring, you would define a bean in your coldspring.xml file for the facade like this:
Using the ValidateThis Facade Object Unless you are integrating the framework directly into your business objects, you use the framework by calling methods on the ValidateThis Facade Object. The following methods are available, listed in order of how often they might be used:
The validate() Method
Use the validate() method to perform server-side validations on an object.
Arguments
It accepts the following arguments:
theObject The actual object to validate. Note that this can also be a structure.
objectType (optional) The name of the object type as defined to the framework. For more information on when this argument is optional, see the section on Specifying the objectType on a Method Call.
context (optional) The name of a context used in the object’s rules definition file.
If passed, the object will be validated using the specified context. Only rules assigned to that context will be evaluated.
Result (optional) A Result object that pre-exists.
If not passed in, a new Result object will be created and returned.
If an existing Result object is passed in, it will be appended to during this validation operation. This is useful when one needs to validate multiple objects during a given operation.
objectList (optional) An array of objects that have already been validated. This is used internally by the isValidObject validator.
debuggingMode (optional) Whether debugging should be turned on or not.
If debugging is on then debug information is gathered and returned via the Result object.
Valid values are none (the default), strict (which will throw an exception if an invalid xml file is encountered, and on which will log all debug information, including invalid xml files.
ignoreMissingProperties (optional) Whether VT should ignore any properties for which rules have been defined but do not exist in the current object. The default is false, which means that missing properties will generate an exception.
locale (optional) The locale to be used when generating default failure messages. Defaults to the defaultLocale value in the ValidateThisConfig Struct.
Return Value
The validate() method returns a Result object, which can be interrogated and used to report validation failures. More details on the Result object can be found in the Working with the Result Object section.
Example
The getValidationScript() Method
Use the getValidationScript() method to return JavaScript code for client-side validations.
Arguments
It accepts the following arguments:
theObject (optional) The actual object for which to generate client-side validations.
objectType (optional) The name of the object type as defined to the framework.
One of either theObject or objectType must be specified, so the framework can identify for which object the script should be returned.
For more information on why both are optional but at least one is required, see the section on Specifying the objectType on a Method Call.
context (optional) The name of a context used in the object’s rules definition file.
If passed, the object will be validated using the specified context. Only rules assigned to that context will be evaluated.
formName (optional) The name of the form in the html document to which validations are to be attached.
If not passed in the framework will use one of two values:
If a formName is associated with the given context in your Rules Definition File via the If one does not want the framework to include the
The getRequiredFields() Method
Use the getRequiredFields() to return a struct that contains keys for each of the form fields that are required for a given object. This is useful if one wants to dynamically indicate those fields on a form, for example by placing an asterisk or an icon in front of each field.
Arguments
It accepts the following arguments:
theObject (optional) The actual object for which to generate client-side validations.
objectType (optional) The name of the object type as defined to the framework.
One of either theObject or objectType must be specified, so the framework can identify for which object the fields should be returned.
For more information on why both are optional but at least one is required, see the section on Specifying the objectType on a Method Call.
context (optional) The name of a context used in the object’s rules definition file.
If passed, the object will be validated using the specified context. Only rules assigned to that context will be evaluated.
Return Value
The getRequiredFields() method returns a struct that contains one key for each required field.
Example
The getRequiredProperties() Method
This method is identical to the getRequiredFields() method, except it returns a struct of property names, rather than a struct of form field names. It is used internally by the framework and does not necessarily have a use case in your application code, but it is available if a use is discovered for it. The getAllContexts() Method
This method can be used to return all of the metadata about validations for a given object. It does not have a use case for application development, but can be very useful in debugging issues with your validation rules.
Arguments
It accepts the following arguments:
theObject (optional) The actual object for which to generate client-side validations.
objectType (optional) The name of the object type as defined to the framework.
One of either theObject or objectType must be specified, so the framework can identify for which object the fields should be returned.
For more information on why both are optional but at least one is required, see the section on Specifying the objectType on a Method Call.
Return Value
The getAllContexts() method returns a struct with one key per context, each of which contains the metadata for all rules defined for the object.
Example
The addRule() Method
The addRule() method can be used to dynamically add validation rules for an object. For more information on why you might choose to do that, and how to call the method, see the section on Defining Validation Rules. The newResult() Method
The newResult() method can be used to return a new, empty Result object from the framework. This is generally unnecessary as the framework returns a Result object from all calls to validate(), but if a use case arises, this method is available.
Arguments
This method accepts no arguments.
Return Value
The newResult() method returns a Result object. More details on the Result object can be found in the Working with the Result Object section.
Example
The getVersion() Method
The getVersion() method will return the version number of the framework. This can be useful when reporting problems with the framework.
Arguments
This method accepts no arguments.
Return Value
The getVersion() method returns a string giving you the number of the installed version.
Example