|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.opensymphony.xwork.validator.ValidatorFactory
public class ValidatorFactory
ValidatorFactory is responsible for
ValidatorConfig
Validation rules are handled by validators, which must be registered with the ValidatorFactory (using the registerValidator method). The simplest way to do so is to add a file name validators.xml in the root of the classpath (/WEB-INF/classes) that declares all the validators you intend to use. As of XWork 1.2.4 / WebWork 2.2.7 and above, validator definition are loaded in the following order, where validators loaded latter could override those loaded previously if there have the same name :-
INFORMATION Normally, to have custom validators, we could have them defined in validators.xml that resides in the classpath. However since, WebWork 2.2.7 / XWork 1.2.4 and above, we could have *-validators.xml that lies in the root of the classpath and WebWork / XWork will pick it up. This allows us to have various validator definition that lies in a jar file itself (at the root of course). To override a validator definition defined by XWork, we could just define a a custom validator mean to replace it with the same name and have them in either validators.xml or *-validators.xml that lies in the root of the classpath (could be in the root of a jar file as well). See ValidatorFactory static block for details.
Note:
The default validationWorkflowStack already includes this.
All that is required to enable validation for an Action is to put the
ValidationInterceptor in the interceptor refs of the action (see xwork.xml) like so:
<interceptor name="validator" class="com.opensymphony.xwork.validator.ValidationInterceptor"/>
Field Validators Field validators, as the name indicate, act on single fields accessible through an action. A validator, in contrast, is more generic and can do validations in the full action context, involving more than one field (or even no field at all) in validation rule. Most validations can be defined on per field basis. This should be preferred over non-field validation whereever possible, as field validator messages are bound to the related field and will be presented next to the corresponding input element in the respecting view.
Non Field Validators Non-field validators only add action level messages. Non-field validators are mostly domain specific and therefore offer custom implementations. The most important standard non-field validator provided by XWork/WebWork is ExpressionValidator.
NOTE: Non-field validators takes precedence over field validators regardless of the order they are defined in *-validation.xml. If a non-field validator is short-circuited, it will causes its non-field validator to not being executed. See validation framework documentation for more info.
VALIDATION RULES: Validation rules can be specified:
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <validators> <field name="bar"> <field-validator type="required"> <message>You must enter a value for bar.</message> </field-validator> <field-validator type="int"> <param name="min">6</param> <param name="max">10</param> <message>bar must be between ${min} and ${max}, current value is ${bar}.</message> </field-validator> </field> <field name="bar2"> <field-validator type="regex"> <param name="regex">[0-9],[0-9]</param> <message>The value of bar2 must be in the format "x, y", where x and y are between 0 and 9</message> </field-validator> </field> <field name="date"> <field-validator type="date"> <param name="min">12/22/2002</param> <param name="max">12/25/2002</param> <message>The date must be between 12-22-2002 and 12-25-2002.</message> </field-validator> </field> <field name="foo"> <field-validator type="int"> <param name="min">0</param> <param name="max">100</param> <message key="foo.range">Could not find foo.range!</message> </field-validator> </field> <validator type="expression"> <param name="expression">foo lt bar </param> <message>Foo must be greater than Bar. Foo = ${foo}, Bar = ${bar}.</message> </validator> </validators>
Here we can see the configuration of validators for the SimpleAction class. Validators (and field-validators) must have a type attribute, which refers to a name of an Validator registered with the ValidatorFactory as above. Validator elements may also have <param> elements with name and value attributes to set arbitrary parameters into the Validator instance. See below for discussion of the message element.
Each Validator or Field-Validator element must define one message element inside the validator element body. The message element has 1 attributes, key which is not required. The body of the message tag is taken as the default message which should be added to the Action if the validator fails.Key gives a message key to look up in the Action's ResourceBundles using getText() from LocaleAware if the Action implements that interface (as ActionSupport does). This provides for Localized messages based on the Locale of the user making the request (or whatever Locale you've set into the LocaleAware Action). After either retrieving the message from the ResourceBundle using the Key value, or using the Default message, the current Validator is pushed onto the ValueStack, then the message is parsed for \$\{...\} sections which are replaced with the evaluated value of the string between the \$\{ and \}. This allows you to parameterize your messages with values from the Validator, the Action, or both.
If the validator fails, the validator is pushed onto the ValueStack and the message - either the default or the locale-specific one if the key attribute is defined (and such a message exists) - is parsed for ${...} sections which are replaced with the evaluated value of the string between the ${ and }. This allows you to parameterize your messages with values from the validator, the Action, or both.
NOTE:Since validation rules are in an XML file, you must make sure you escape special characters. For example, notice that in the expression validator rule above we use ">" instead of ">". Consult a resource on XML for the full list of characters that must be escaped. The most commonly used characters that must be escaped are: & (use &), > (user >), and < (use <).
Here is an example of a parameterized message:
This will pull the min and max parameters from the IntRangeFieldValidator and the value of bar from the Action.
bar must be between ${min} and ${max}, current value is ${bar}.
Method Summary | |
---|---|
static Validator |
getValidator(ValidatorConfig cfg)
Get a Validator that matches the given configuration. |
static String |
lookupRegisteredValidatorType(String name)
Lookup to get the FQ classname of the given validator name. |
static void |
parseValidators()
Parse all the validators definition, using the following precedence:- Defaults from com/opensymphony/xwork/validator/validators/default.xml validators.xml that lies in the root of the classpath *-validators.xml that lies in the root of the classpath |
static void |
registerValidator(String name,
String className)
Registers the given validator to the existing map of validators. |
static void |
reset()
Removed all registered validators. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static void reset()
public static Validator getValidator(ValidatorConfig cfg)
cfg
- the configurator.
public static void registerValidator(String name, String className)
name
- name of validator to add.className
- the FQ classname of the validator.public static String lookupRegisteredValidatorType(String name)
name
- name of validator to lookup.
IllegalArgumentException
- is thrown if the name is not found.public static void parseValidators() throws IOException, URISyntaxException
IOException
URISyntaxException
|
WebWork Project Page | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |