org.springmodules.validation.valang
Class ValangValidator

java.lang.Object
  extended by org.springmodules.validation.util.context.BasicContextAware
      extended by org.springmodules.validation.valang.parser.SimpleValangBased
          extended by org.springmodules.validation.valang.ValangValidator
All Implemented Interfaces:
ContextAware, ValangBased

public class ValangValidator
extends SimpleValangBased

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>
  
 

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.

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

Field Summary
 
Fields inherited from class org.springmodules.validation.util.context.BasicContextAware
applicationContext, applicationEventPublisher, beanFactory, messageSource, resourceLoader, servletContext
 
Constructor Summary
ValangValidator()
           
 
Method Summary
 void afterPropertiesSet()
           
 java.util.Collection getRules()
           
 void setCustomPropertyEditors(java.util.Collection customPropertyEditors)
           Sets custom property editors on BeanWrapper instances (optional).
 void setDateParserRegistrations(java.util.Map dateParserRegistrations)
          Deprecated. Use SimpleValangBased.setDateParsers(java.util.Map) instead.
 void setValang(java.lang.String valang)
           This property sets the Valang syntax.
 boolean supports(java.lang.Class clazz)
           
 void validate(java.lang.Object target, Errors errors)
           
 
Methods inherited from class org.springmodules.validation.valang.parser.SimpleValangBased
addCustomFunction, createValangParser, findAllCustomFunctionsInApplicationContext, getAllCustomFunctions, getCustomFunctions, getDateParsers, initValang, setCustomFunctions, setDateParsers
 
Methods inherited from class org.springmodules.validation.util.context.BasicContextAware
initLifecycle, initLifecycle, setApplicationContext, setApplicationEventPublisher, setBeanFactory, setMessageSource, setResourceLoader, setServletContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ValangValidator

public ValangValidator()
Method Detail

setValang

public void setValang(java.lang.String valang)

This property sets the Valang syntax.

Parameters:
valang - the Valang syntax

setCustomPropertyEditors

public void setCustomPropertyEditors(java.util.Collection customPropertyEditors)

Sets custom property editors on BeanWrapper instances (optional).

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

setDateParserRegistrations

public void setDateParserRegistrations(java.util.Map dateParserRegistrations)
Deprecated. Use SimpleValangBased.setDateParsers(java.util.Map) instead.

Sets date parser registrations (formats and modifiers) on DefaultDateParser (optional).

Parameters:
dateParserRegistrations - the date parser registrations
See Also:
DefaultDateParser.register(String, String), DefaultDateParser.register(String, DateModifier)

getRules

public java.util.Collection getRules()

afterPropertiesSet

public void afterPropertiesSet()
                        throws java.lang.Exception
Throws:
java.lang.Exception

supports

public boolean supports(java.lang.Class clazz)

validate

public void validate(java.lang.Object target,
                     Errors errors)


Copyright © 2005. All Rights Reserved.