📄 validator.java
字号:
/* * Copyright (c) 2002-2006 by OpenSymphony * All rights reserved. */package com.opensymphony.xwork2.validator;/** * <!-- START SNIPPET: validatorFlavours --> * <p>The validators supplied by the Xwork distribution (and any validators you * might write yourself) come in two different flavors:</p> * * <ol> * <li> Plain Validators / Non-Field validators </li> * <li> FieldValidators </li> * </ol> * * <p>Plain Validators (such as the ExpressionValidator) perform validation checks * that are not inherently tied to a single specified field. When you declare a * plain Validator in your -validation.xml file you do not associate a fieldname * attribute with it. (You should avoid using plain Validators within the * <field-validator> syntax described below.)</p> * * <p>FieldValidators (such as the EmailValidator) are designed to perform * validation checks on a single field. They require that you specify a fieldname * attribute in your -validation.xml file. There are two different (but equivalent) * XML syntaxes you can use to declare FieldValidators (see "<validator> vs. * <field-Validator> syntax" below).</p> * * <p>There are two places where the differences between the two validator flavors * are important to keep in mind:</p> * * <ol> * <li> when choosing the xml syntax used for declaring a validator * (either <validator> or <field-validator>)</li> * <li> when using the short-circuit capability</li> * </ol> * * <p><b>NOTE:</b>Note that you do not declare what "flavor" of validator you are * using in your -validation.xml file, you just declare the name of the validator * to use and WebWork will know whether it's a "plain Validator" or a "FieldValidator" * by looking at the validation class that the validator's programmer chose * to implement.</p> * <!-- END SNIPPET: validatorFlavours --> * * * * * <!-- START SNIPPET: validationRules --> * <p>To define validation rules for an Action, create a file named ActionName-validation.xml * in the same package as the Action. You may also create alias-specific validation rules which * add to the default validation rules defined in ActionName-validation.xml by creating * another file in the same directory named ActionName-aliasName-validation.xml. In both * cases, ActionName is the name of the Action class, and aliasName is the name of the * Action alias defined in the xwork.xml configuration for the Action.</p> * * <p>The framework will also search up the inheritance tree of the Action to * find validation rules for directly implemented interfaces and parent classes of the Action. * This is particularly powerful when combined with ModelDriven Actions and the VisitorFieldValidator. * Here's an example of how validation rules are discovered. Given the following class structure:</p> * * <ul> * <li>interface Animal;</li> * <li>interface Quadraped extends Animal;</li> * <li>class AnimalImpl implements Animal;</li> * <li>class QuadrapedImpl extends AnimalImpl implements Quadraped;</li> * <li>class Dog extends QuadrapedImpl;</li> * </ul> * * <p>The framework method will look for the following config files if Dog is to be validated:</p> * * <ul> * <li>Animal</li> * <li>Animal-aliasname</li> * <li>AnimalImpl</li> * <li>AnimalImpl-aliasname</li> * <li>Quadraped</li> * <li>Quadraped-aliasname</li> * <li>QuadrapedImpl</li> * <li>QuadrapedImpl-aliasname</li> * <li>Dog</li> * <li>Dog-aliasname</li> * </ul> * * <p>While this process is similar to what the XW:Localization framework does * when finding messages, there are some subtle differences. The most important * difference is that validation rules are discovered from the parent downwards. * </p> * * <p><b>NOTE:</b>Child's *-validation.xml will add on to parent's *-validation.xml * according to the class hierarchi defined above. With this feature, one could have * more generic validation rule at the parent and more specific validation rule at * the child.</p> * * <!-- END SNIPPET: validationRules --> * * * <!-- START SNIPPET: validatorVsFieldValidators1 --> * <p>There are two ways you can define validators in your -validation.xml file:</p> * <ol> * <li> <validator> </li> * <li> <field-validator> </li> * </ol> * <p>Keep the following in mind when using either syntax:</p> * * <p><b>Non-Field-Validator</b> * The <validator> element allows you to declare both types of validators * (either a plain Validator a field-specific FieldValidator).</p> * <!-- END SNIPPET: validatorVsFieldValidators1 --> * *<pre> * <!-- START SNIPPET: nonFieldValidatorUsingValidatorSyntax --> * <!-- Declaring a plain Validator using the <validator> syntax: --> * * <validator type="expression> * <param name="expression">foo gt bar</param> * <message>foo must be great than bar.</message> * </validator> * <!-- END SNIPPET: nonFieldValidatorUsingValidatorSyntax --> * </pre> * * <pre> * <!-- START SNIPPET: fieldValidatorUsingValidatorSyntax --> * <!-- Declaring a field validator using the <validator> syntax; --> * * <validator type="required"> * <param name="fieldName">bar</param> * <message>You must enter a value for bar.</message> * </validator> * <!-- END SNIPPET: fieldValidatorUsingValidatorSyntax --> * </pre> * * * <!-- START SNIPPET: validatorVsFieldValidators2 --> * <p><b>field-validator</b> * The <field-validator> elements are basically the same as the <validator> elements * except that they inherit the fieldName attribute from the enclosing <field> element. * FieldValidators defined within a <field-validator> element will have their fieldName * automatically filled with the value of the parent <field> element's fieldName * attribute. The reason for this structure is to conveniently group the validators * for a particular field under one element, otherwise the fieldName attribute * would have to be repeated, over and over, for each individual <validator>.</p> * * <p><b>HINT:</b> * It is always better to defined field-validator inside a <field> tag instead of * using a <validator> tag and supplying fieldName as its param as the xml code itself * is clearer (grouping of field is clearer)</p> * * <p><b>NOTE:</b> * Note that you should only use FieldValidators (not plain Validators) within a * <field-validator> block. A plain Validator inside a <field> will not be * allowed and would generate error when parsing the xml, as it is not allowed in * the defined dtd (xwork-validator-1.0.2.dtd)</p> * <!-- END SNIPPET: validatorVsFieldValidators2 --> * * <pre> * <!-- START SNIPPET: fieldValidatorUsingFieldValidatorSyntax --> * Declaring a FieldValidator using the <field-validator> syntax: * * <field name="email_address"> * <field-validator type="required"> * <message>You cannot leave the email address field empty.</message> * </field-validator> * <field-validator type="email"> * <message>The email address you entered is not valid.</message> * </field-validator> * </field> * <!-- END SNIPPET: fieldValidatorUsingFieldValidatorSyntax --> * </pre> * * * <!-- START SNIPPET: validatorVsFieldValidators3 --> * <p>The choice is yours. It's perfectly legal to only use <validator> elements * without the <field> elements and set the fieldName attribute for each of them. * The following are effectively equal:</P> * <!-- END SNIPPET: validatorVsFieldValidators3 --> * * <pre> * <!-- START-SNIPPET: similarVaidatorDeclaredInDiffSyntax --> * <field name="email_address"> * <field-validator type="required"> * <message>You cannot leave the email address field empty.</message> * </field-validator> * <field-validator type="email">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -