com.opensymphony.xwork2.validator.validators
Class VisitorFieldValidator

java.lang.Object
  extended by com.opensymphony.xwork2.validator.validators.ValidatorSupport
      extended by com.opensymphony.xwork2.validator.validators.FieldValidatorSupport
          extended by com.opensymphony.xwork2.validator.validators.VisitorFieldValidator
All Implemented Interfaces:
FieldValidator, ShortCircuitableValidator, Validator
Direct Known Subclasses:
ConditionalVisitorFieldValidator

public class VisitorFieldValidator
extends FieldValidatorSupport

The VisitorFieldValidator allows you to forward validation to object properties of your action using the object's own validation files. This allows you to use the ModelDriven development pattern and manage your validations for your models in one place, where they belong, next to your model classes. The VisitorFieldValidator can handle either simple Object properties, Collections of Objects, or Arrays.

 
    <validators>
        <!-- Plain Validator Syntax -->
        <validator type="visitor">
            <param name="fieldName">user</param>
            <param name="context">myContext</param>
            <param name="appendPrefix">true</param>
        </validator>

        <!-- Field Validator Syntax -->
        <field name="user">
           <field-validator type="visitor">
              <param name="context">myContext</param>
              <param name="appendPrefix">true</param>
           </field-validator>
        </field>
    </validators>
 
 

In the example above, if the acion's getUser() method return User object, XWork will look for User-myContext-validation.xml for the validators. Since appednPrefix is true, every field name will be prefixed with 'user' such that if the actual field name for 'name' is 'user.name'

Version:
$Date: 2008-03-28 16:57:30 +0100 (Fri, 28 Mar 2008) $ $Id: VisitorFieldValidator.java 1765 2008-03-28 15:57:30Z rainerh $
Author:
Jason Carreira, Rainer Hermanns

Nested Class Summary
static class VisitorFieldValidator.AppendingValidatorContext
           
 
Field Summary
private  ActionValidatorManager actionValidatorManager
           
private  boolean appendPrefix
           
private  String context
           
 
Fields inherited from class com.opensymphony.xwork2.validator.validators.ValidatorSupport
defaultMessage, log, messageKey
 
Constructor Summary
VisitorFieldValidator()
           
 
Method Summary
 String getContext()
           
 boolean isAppendPrefix()
          Flags whether the field name of this field validator should be prepended to the field name of the visited field to determine the full field name when an error occurs.
 void setActionValidatorManager(ActionValidatorManager mgr)
           
 void setAppendPrefix(boolean appendPrefix)
          Sets whether the field name of this field validator should be prepended to the field name of the visited field to determine the full field name when an error occurs.
 void setContext(String context)
           
 void validate(Object object)
          The validation implementation must guarantee that setValidatorContext will be called with a non-null ValidatorContext before validate is called.
private  void validateArrayElements(Object[] array, String fieldName, String visitorContext)
           
private  void validateObject(String fieldName, Object o, String visitorContext)
           
 
Methods inherited from class com.opensymphony.xwork2.validator.validators.FieldValidatorSupport
getFieldName, getValidatorType, setFieldName, setValidatorType
 
Methods inherited from class com.opensymphony.xwork2.validator.validators.ValidatorSupport
addActionError, addFieldError, conditionalParse, getDefaultMessage, getFieldValue, getMessage, getMessageKey, getParse, getValidatorContext, isShortCircuit, setDefaultMessage, setMessageKey, setParse, setShortCircuit, setValidatorContext, setValueStack
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.opensymphony.xwork2.validator.Validator
getDefaultMessage, getMessage, getMessageKey, getValidatorContext, setDefaultMessage, setMessageKey, setValidatorContext, setValueStack
 

Field Detail

context

private String context

appendPrefix

private boolean appendPrefix

actionValidatorManager

private ActionValidatorManager actionValidatorManager
Constructor Detail

VisitorFieldValidator

public VisitorFieldValidator()
Method Detail

setActionValidatorManager

public void setActionValidatorManager(ActionValidatorManager mgr)

setAppendPrefix

public void setAppendPrefix(boolean appendPrefix)
Sets whether the field name of this field validator should be prepended to the field name of the visited field to determine the full field name when an error occurs. The default is true.


isAppendPrefix

public boolean isAppendPrefix()
Flags whether the field name of this field validator should be prepended to the field name of the visited field to determine the full field name when an error occurs. The default is true.


setContext

public void setContext(String context)

getContext

public String getContext()

validate

public void validate(Object object)
              throws ValidationException
Description copied from interface: Validator
The validation implementation must guarantee that setValidatorContext will be called with a non-null ValidatorContext before validate is called.

Parameters:
object - the object to be validated.
Throws:
ValidationException - is thrown if there is validation error(s).

validateArrayElements

private void validateArrayElements(Object[] array,
                                   String fieldName,
                                   String visitorContext)
                            throws ValidationException
Throws:
ValidationException

validateObject

private void validateObject(String fieldName,
                            Object o,
                            String visitorContext)
                     throws ValidationException
Throws:
ValidationException


Copyright © 2000-2008 Apache Software Foundation. All Rights Reserved.