org.springmodules.validation.valang
Class ValangValidator

java.lang.Object
  extended by org.springmodules.validation.valang.parser.SimpleValangBased
      extended by org.springmodules.validation.valang.ValangValidator
All Implemented Interfaces:
InitializingBean, ApplicationContextAware, Validator, ValangBased

public class ValangValidator
extends SimpleValangBased
implements Validator, InitializingBean

An implementation of Validator that takes a Valang syntax string to define the set of validation rules it will apply. This instance is thread-safe.

The syntax of a Valang instruction is:

 

{ <key> : <expression> : <error_message> [ : <error_key> [ : <error_args> ] ] }

These instructions can be repeated and will be combined in a Validator instance. Each instruction will execute the expression on a target bean. If the expression fails the key will be rejected with the error message, error key and error arguments. If no error key is provided the key will be used as error key.

Some examples of the Valang syntax:

 

<bean id="myValidator" class="org.springmodules.validation.valang.ValangValidatorFactoryBean"> <property name="valang"><value><![CDATA[ { age : ? is not null : 'Age is a required field.' : 'age_required' } { age : ? is null or ? >= minAge : 'Customers must be {0} years or older.' : 'not_old_enough' : minAge } { valueDate : ? is not null : 'Value date is a required field.' : 'valueDate_required' } { valueDate : ? is null or (? >= [T<d] and [T>d] > ?) : 'Value date must be today.' : 'valueDate_today' } { firstName : ? has text : 'First name is a required field.' : 'firstName_required' } { firstName : ? has no text or length(firstName) <= 50 : 'First name must be no longer than {0} characters.' : 'firstName_length' : 50 } { size : ? has length : 'Size is a required field.' } { size : ? has no length or upper(?) in 'S', 'M', 'L', 'XL' : 'Size must be either {0}, {1}, {2} or {3}.' : 'size_error' : 'S', 'M', 'L', 'XL' } { lastName : ? has text and !(false) = true : 'Last name is required and not false must be true.' } ]]></value></property> </bean>

Enums can be dynamically resolved either based on comparing an enum type to a literal delimitted by "['" + <enum> + "']" or it will be directly resovled if the complete path is specified.

 

<bean id="myValidator" class="org.springmodules.validation.valang.ValangValidatorFactoryBean"> <property name="valang"><value><![CDATA[ { personType : ? EQUALS ['STUDENT'] : 'Person type must be student.' } { personType : personType EQUALS ['org.springmodules.validation.example.PersonType.STUDENT'] : 'Person type must be student.' } { personType : personType EQUALS ['org.springmodules.validation.example.Person$PersonType.STUDENT'] : 'Person type must be student.' } ]]></value></property> </bean>

Where clauses are very similar to a SQL WHERE clause. It lets you short circuit the validation in case there are some rules that should only be applied after other criteria have been satisfied. A where clause doesn't generate any errors, and a where clause is optional.

 

<bean id="myValidator" class="org.springmodules.validation.valang.ValangValidatorFactoryBean"> <property name="valang"><value><![CDATA[ { age : ? > 18 WHERE lastName EQUALS 'Smith' : 'Age must be greater than 18 if your last name is Smith.' : 'valueDate_required' } ]]></value></property> </bean>

Custom property editors can be registered using org.springmodules.validation.valang.CustomPropertyEditor.

A custom visitor can be registered to use custom functions in the Valang syntax.

If the class name is set it will be be used for bytecode generation to avoid reflection.

Note: By specifying the class name the Validator will only be able to validate the class specified

Since:
23-04-2005
Author:
Steven Devijver
See Also:
DefaultDateParser, Validator

Constructor Summary
ValangValidator()
          Constructor
 
Method Summary
 void afterPropertiesSet()
          Implementation of InitializingBean.
 Collection<ValidationRule> getRules()
          Gets list of validation rules.
 void setClassName(String className)
          Sets the class name to be used for bytecode generation to avoid reflection.
 void setCustomPropertyEditors(Collection<CustomPropertyEditor> customPropertyEditors)
          Sets custom property editors on BeanWrapper instances (optional).
 void setValang(String valang)
          This property sets the Valang syntax.
 boolean supports(Class clazz)
          What validation class is supported.
 void validate(Object target, Errors errors)
          Validate the supplied target object, which must be of a Class for which the supports(Class) method typically has (or would) return true.
 
Methods inherited from class org.springmodules.validation.valang.parser.SimpleValangBased
addCustomFunction, createValangParser, createValangParser, findAllCustomFunctionsInApplicationContext, getAllCustomFunctions, getCustomFunctions, getDateParsers, initValang, setApplicationContext, setCustomFunctions, setDateParsers
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ValangValidator

public ValangValidator()
Constructor

Method Detail

setValang

public void setValang(String valang)
This property sets the Valang syntax.

Parameters:
valang - The Valang validation expression.

setClassName

public void setClassName(String className)

Sets the class name to be used for bytecode generation to avoid reflection.

Note: By specifying this Validator will only be able to validate the class specified

Parameters:
className - The fully qualified class name to perform validation on.

setCustomPropertyEditors

public void setCustomPropertyEditors(Collection<CustomPropertyEditor> customPropertyEditors)
Sets custom property editors on BeanWrapper instances (optional).

Parameters:
customPropertyEditors - the custom editors.
See Also:
PropertyEditorRegistry.registerCustomEditor(Class, String, java.beans.PropertyEditor), PropertyEditorRegistry.registerCustomEditor(Class, java.beans.PropertyEditor)

getRules

public Collection<ValidationRule> getRules()
Gets list of validation rules.


afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Implementation of InitializingBean.

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
Exception

supports

public boolean supports(Class clazz)
What validation class is supported. Implementation of Validator.

Specified by:
supports in interface Validator

validate

public void validate(Object target,
                     Errors errors)

Validate the supplied target object, which must be of a Class for which the supports(Class) method typically has (or would) return true.

Implementation of Validator.

Specified by:
validate in interface Validator


Copyright © 2009. All Rights Reserved.