Save This Page
Home » xwork-2.1.5 » com.opensymphony » xwork2 » validator » [javadoc | source]
    1   package com.opensymphony.xwork2.validator;
    2   
    3   /**
    4    * ValidatorFactory
    5    *
    6    * <p>
    7    * <!-- START SNIPPET: javadoc -->
    8    * Validation rules are handled by validators, which must be registered with
    9    * the ValidatorFactory (using the registerValidator method). The simplest way to do so is to add a file name
   10    * validators.xml in the root of the classpath (/WEB-INF/classes) that declares
   11    * all the validators you intend to use.
   12    * <!-- END SNIPPET: javadoc -->
   13    * </p>
   14    *
   15    *
   16    * <p>
   17    * <b>INFORMATION</b>
   18    * <!-- START SNIPPET: information -->
   19    * validators.xml if being defined should be available in the classpath. However
   20    * this is not necessary, if no custom validator is needed. Predefined sets of validators
   21    * will automatically be picked up when defined in
   22    * com/opensymphony/xwork2/validator/validators/default.xml packaged in
   23    * in the xwork jar file. See ValidatorFactory static block for details.
   24    * <!-- END SNIPPET: information -->
   25    * </p>
   26    *
   27    * <p>
   28    * <b>WARNING</b>
   29    * <!-- START SNIPPET: warning -->
   30    * If custom validator is being defined and a validators.xml is created and
   31    * place in the classpath, do remember to copy all the other pre-defined validators
   32    * that is needed into the validators.xml as if not they will not be registered.
   33    * Once a validators.xml is detected in the classpath, the default one
   34    * (com/opensymphony/xwork2/validator/validators/default.xml) will not be loaded.
   35    * It is only loaded when a custom validators.xml cannot be found in the classpath.
   36    *  Be careful.
   37    * <!-- END SNIPPET: warning -->
   38    * </p>
   39    *
   40    * <p><b>Note:</b>
   41    * <!-- START SNIPPET: turningOnValidators -->
   42    * The default validationWorkflowStack already includes this.<br/>
   43    * All that is required to enable validation for an Action is to put the
   44    * ValidationInterceptor in the interceptor refs of the action (see xwork.xml) like so:
   45    * <!-- END SNIPPET: turningOnValidators -->
   46    * </p>
   47    *
   48    * <pre>
   49    * <!-- START SNIPPET: exTurnOnValidators -->
   50    *     &lt;interceptor name="validator" class="com.opensymphony.xwork2.validator.ValidationInterceptor"/&gt;
   51    * <!-- END SNIPPET: exTurnOnValidators -->
   52    * </pre>
   53    *
   54    * <p><b>Field Validators</b>
   55    * <!-- START SNIPPET: fieldValidators -->
   56    * Field validators, as the name indicate, act on single fields accessible through an action.
   57    * A validator, in contrast, is more generic and can do validations in the full action context,
   58    * involving more than one field (or even no field at all) in validation rule.
   59    * Most validations can be defined on per field basis. This should be preferred over
   60    * non-field validation wherever possible, as field validator messages are bound to the
   61    * related field and will be presented next to the corresponding input element in the
   62    * respecting view.
   63    * <!-- END SNIPPET: fieldValidators -->
   64    * </p>
   65    *
   66    * <p><b>Non Field Validators</b>
   67    * <!-- START SNIPPET: nonFieldValidators -->
   68    * Non-field validators only add action level messages. Non-field validators
   69    * are mostly domain specific and therefore offer custom implementations.
   70    * The most important standard non-field validator provided by XWork
   71    * is ExpressionValidator.
   72    * <!-- END SNIPPET: nonFieldValidators -->
   73    * </p>
   74    *
   75    * <p><b>NOTE:</b>
   76    * <!-- START SNIPPET: validatorsNote -->
   77    * Non-field validators takes precedence over field validators
   78    * regardless of the order they are defined in *-validation.xml. If a non-field
   79    * validator is short-circuited, it will causes its non-field validator to not
   80    * being executed. See validation framework documentation for more info.
   81    * <!-- END SNIPPET: validatorsNote -->
   82    * </p>
   83    *
   84    * <p><b>VALIDATION RULES:</b>
   85    * <!-- START SNIPPET: validationRules1 -->
   86    * Validation rules can be specified:
   87    * <ol>
   88    *  <li> Per Action class: in a file named ActionName-validation.xml</li>
   89    *  <li> Per Action alias: in a file named ActionName-alias-validation.xml</li>
   90    *  <li> Inheritance hierarchy and interfaces implemented by Action class:
   91    *  XWork searches up the inheritance tree of the action to find default
   92    *  validations for parent classes of the Action and interfaces implemented</li>
   93    * </ol>
   94    * Here is an example for SimpleAction-validation.xml:
   95    * <!-- END SNIPPET: validationRules1 -->
   96    * <p>
   97    *
   98    * <pre>
   99    * <!-- START SNIPPET: exValidationRules1 -->
  100    * &lt;!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
  101    *        "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"&gt;
  102    * &lt;validators&gt;
  103    *   &lt;field name="bar"&gt;
  104    *       &lt;field-validator type="required"&gt;
  105    *           &lt;message&gt;You must enter a value for bar.&lt;/message&gt;
  106    *       &lt;/field-validator&gt;
  107    *       &lt;field-validator type="int"&gt;
  108    *           &lt;param name="min">6&lt;/param&gt;
  109    *           &lt;param name="max"&gt;10&lt;/param&gt;
  110    *           &lt;message&gt;bar must be between ${min} and ${max}, current value is ${bar}.&lt;/message&gt;
  111    *       &lt;/field-validator&gt;
  112    *   &lt;/field&gt;
  113    *   &lt;field name="bar2"&gt;
  114    *       &lt;field-validator type="regex"&gt;
  115    *           &lt;param name="expression"&gt;[0-9],[0-9]&lt;/param&gt;
  116    *           &lt;message&gt;The value of bar2 must be in the format "x, y", where x and y are between 0 and 9&lt;/message&gt;
  117    *      &lt;/field-validator&gt;
  118    *   &lt;/field&gt;
  119    *   &lt;field name="date"&gt;
  120    *       &lt;field-validator type="date"&gt;
  121    *           &lt;param name="min"&gt;12/22/2002&lt;/param&gt;
  122    *           &lt;param name="max"&gt;12/25/2002&lt;/param&gt;
  123    *           &lt;message&gt;The date must be between 12-22-2002 and 12-25-2002.&lt;/message&gt;
  124    *       &lt;/field-validator&gt;
  125    *   &lt;/field&gt;
  126    *   &lt;field name="foo"&gt;
  127    *       &lt;field-validator type="int"&gt;
  128    *           &lt;param name="min"&gt;0&lt;/param&gt;
  129    *           &lt;param name="max"&gt;100&lt;/param&gt;
  130    *           &lt;message key="foo.range"&gt;Could not find foo.range!&lt;/message&gt;
  131    *       &lt;/field-validator&gt;
  132    *   &lt;/field&gt;
  133    *   &lt;validator type="expression"&gt;
  134    *       &lt;param name="expression"&gt;foo lt bar &lt;/param&gt;
  135    *       &lt;message&gt;Foo must be greater than Bar. Foo = ${foo}, Bar = ${bar}.&lt;/message&gt;
  136    *   &lt;/validator&gt;
  137    * &lt;/validators&gt;
  138    * <!-- END SNIPPET: exValidationRules1 -->
  139    * </pre>
  140    *
  141    *
  142    * <p>
  143    * <!-- START SNIPPET: validationRules2 -->
  144    * Here we can see the configuration of validators for the SimpleAction class.
  145    * Validators (and field-validators) must have a type attribute, which refers
  146    * to a name of an Validator registered with the ValidatorFactory as above.
  147    * Validator elements may also have &lt;param&gt; elements with name and value attributes
  148    * to set arbitrary parameters into the Validator instance. See below for discussion
  149    * of the message element.
  150    * <!-- END SNIPPET: validationRules2 -->
  151    * </p>
  152    *
  153    *
  154    *
  155    * <!-- START SNIPPET: validationRules3 -->
  156    * <p>Each Validator or Field-Validator element must define one message element inside
  157    * the validator element body. The message element has 1 attributes, key which is not
  158    * required. The body of the message tag is taken as the default message which should
  159    * be added to the Action if the validator fails. Key gives a message key to look up
  160    * in the Action's ResourceBundles using getText() from LocaleAware if the Action
  161    * implements that interface (as ActionSupport does). This provides for Localized
  162    * messages based on the Locale of the user making the request (or whatever Locale
  163    * you've set into the LocaleAware Action). After either retrieving the message from
  164    * the ResourceBundle using the Key value, or using the Default message, the current
  165    * Validator is pushed onto the ValueStack, then the message is parsed for \$\{...\}
  166    * sections which are replaced with the evaluated value of the string between the
  167    * \$\{ and \}. This allows you to parameterize your messages with values from the
  168    * Validator, the Action, or both.</p>
  169    *
  170    *
  171    * <p>If the validator fails, the validator is pushed onto the ValueStack and the
  172    * message - either the default or the locale-specific one if the key attribute is
  173    * defined (and such a message exists) - is parsed for ${...} sections which are
  174    * replaced with the evaluated value of the string between the ${ and }. This
  175    * allows you to parameterize your messages with values from the validator, the
  176    * Action, or both. </p>
  177    *
  178    * <p><b>NOTE:</b> Since validation rules are in an XML file, you must make sure
  179    * you escape special characters. For example, notice that in the expression
  180    * validator rule above we use "&amp;gt;" instead of "&gt;". Consult a resource on XML
  181    * for the full list of characters that must be escaped. The most commonly used
  182    * characters that must be escaped are: &amp; (use &amp;amp;), &gt; (user &amp;gt;), and &lt; (use &amp;lt;).</p>
  183    *
  184    * <p>Here is an example of a parameterized message:</p>
  185    * <p>This will pull the min and max parameters from the IntRangeFieldValidator and
  186    * the value of bar from the Action.</p>
  187    * <!-- END SNIPPET: validationRules3 -->
  188    *
  189    * <pre>
  190    * <!-- START SNIPPET: exValidationRules3 -->
  191    *    bar must be between ${min} and ${max}, current value is ${bar}.
  192    * <!-- END SNIPPET: exValidationRules3 -->
  193    * </pre>
  194    *
  195    * <!-- START SNIPPET: validationRules4 -->
  196    * <p>Another notable fact is that the provided message value is capable of containing OGNL expressions.
  197    * Keeping this in mind, it is possible to construct quite sophisticated messages.</p>
  198    * <p>See the following example to get an impression:</p>
  199    *
  200    * <!-- END SNIPPET: validationRules4 -->
  201    *
  202    * <pre>
  203    * <!-- START SNIPPET: exValidationRules4 -->
  204    *    <message>${getText("validation.failednotice")}! ${getText("reason")}: ${getText("validation.inputrequired")}</message>
  205    * <!-- END SNIPPET: exValidationRules4 -->
  206    * </pre>
  207    *
  208    * @version $Date$ $Id$
  209    * @author Jason Carreira
  210    * @author James House
  211    */
  212   public interface ValidatorFactory {
  213   
  214       /**
  215        * Get a Validator that matches the given configuration.
  216        *
  217        * @param cfg  the configurator.
  218        * @return  the validator.
  219        */
  220       Validator getValidator(ValidatorConfig cfg);
  221   
  222       /**
  223        * Registers the given validator to the existing map of validators.
  224        * This will <b>add</b> to the existing list.
  225        *
  226        * @param name    name of validator to add.
  227        * @param className   the FQ classname of the validator.
  228        */
  229       void registerValidator(String name, String className);
  230   
  231       /**
  232        * Lookup to get the FQ classname of the given validator name.
  233        *
  234        * @param name   name of validator to lookup.
  235        * @return  the found FQ classname
  236        * @throws IllegalArgumentException is thrown if the name is not found.
  237        */
  238       String lookupRegisteredValidatorType(String name);
  239   }

Save This Page
Home » xwork-2.1.5 » com.opensymphony » xwork2 » validator » [javadoc | source]