|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springmodules.validation.valang.parser.SimpleValangBased org.springmodules.validation.valang.ValangValidator
public class ValangValidator
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.
Custom property editors can be registered using org.springmodules.validation.valang.CustomPropertyEditor.<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>
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
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 |
---|
public ValangValidator()
Method Detail |
---|
public void setValang(String valang)
valang
- The Valang validation expression.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
className
- The fully qualified class name to perform validation on.public void setCustomPropertyEditors(Collection<CustomPropertyEditor> customPropertyEditors)
customPropertyEditors
- the custom editors.PropertyEditorRegistry.registerCustomEditor(Class, String,
java.beans.PropertyEditor)
,
PropertyEditorRegistry.registerCustomEditor(Class,
java.beans.PropertyEditor)
public Collection<ValidationRule> getRules()
public void afterPropertiesSet() throws Exception
InitializingBean
.
afterPropertiesSet
in interface InitializingBean
Exception
public boolean supports(Class clazz)
Validator
.
supports
in interface Validator
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
.
validate
in interface Validator
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |