This page last changed on Jun 03, 2004 by unkyaku.

Validators implement the com.opensymphony.xwork.validator.Validator interface

public interface Validator {
    void setDefaultMessage(String message);

    String getDefaultMessage();

    String getMessage(Object object);

    void setMessageKey(String key);

    String getMessageKey();

    /**
     * This method will be called before validate with a non-null ValidatorContext.
     * @param validatorContext
     */
    void setValidatorContext(ValidatorContext validatorContext);

    ValidatorContext getValidatorContext();

    /**
     * The validation implementation must guarantee that setValidatorContext
     * will be called with a non-null ValidatorContext before validate is called.
     * @param object
     * @throws ValidationException
     */
    void validate(Object object) throws ValidationException;
}

FieldValidators implement com.opensymphony.xwork.validator.FieldValidator, which extends Validator:

public interface FieldValidator extends Validator {

    /**
     * Sets the field name to validate with this FieldValidator
     * @param fieldName
     */
    void setFieldName(String fieldName);

    /**
     * @return the field name to be validated
     */
    String getFieldName();
}

If you want to be able to use the "short-circuit" attribute, you should also implement com.opensymphony.xwork.validator.ShortCircuitableValidator.

Validators and FieldValidators can extend base classes com.opensymphony.xwork.validator.validators.ValidatorSupport and com.opensymphony.xwork.validator.validators.FieldValidatorSupport to get the base message and short-circuiting behavior, and will only need to implement validate(Action action).

The Support classes provide the functionality to use the message key and default message to get the localied message body and the parsing of the message body to provide for parameterized messages. Implementations of the Validator Interface which do not extend the Support base classes should provide this functionality as well for consistency.

The ValidatorContext set into the Validator is an interface which extends both ValidationAware and LocaleAware and is used for looking up message texts and settting errors. When validators are called from the ValidationInterceptor, a DelegatingValidatorContext is created which delegates these calls to the Action if it implements these interfaces. If the Action does not implement LocaleAware, a LocaleAwareSupport instance is created which uses the Action's class to look up resource bundle texts, if available. If the action does not implement ValidationAware, an implementation which simply logs the validation errors is created and delegated to. When calling the validation framework from outside the ValidationInterceptor, any ValidatorContext implementation can be passed in.

Validator classes may define any number of properties using the usual getX() setX() naming convention and have those properties set using <param name="x">foo</param> elements below the <validator> element. The values of these properties may then be used in the validate() method to parameterize the validation. Validators which extend the Support classes may also use the

Object getFieldValue(String name, Action action)

method to get the field value of a named property from an Action.

Document generated by Confluence on Dec 12, 2004 12:35