Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...


Anchor
RequiredFieldValidatorValidationsRequiredFieldValidator
Validations

RequiredFieldValidator

This validator checks that a field is non-null.

Example:

...


@RequiredFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true)

Validations

If you want to use several annotations of the same type, these annotation must be nested within the @Validations() annotation.

The possible parameters for @Validations() are as followed:

Parameter

Required

Notes

requiredFields

no

Add list of RequiredFieldValidators

customValidators

no

Add list of CustomValidators

conversionErrorFields

no

Add list of ConversionErrorFieldValidators

dateRangeFields

no

Add list of DateRangeFieldValidators

emails

no

Add list of EmailValidators

fieldExpressions

no

Add list of FieldExpressionValidators

intRangeFields

no

Add list of IntRangeFieldValidators

requiredFields

no

Add list of RequiredFieldValidators

requiredStrings

no

Add list of RequiredStringValidators

stringLengthFields

no

Add list of StringLengthFieldValidators

urls

no

Add list of UrlValidators

visitorFields

no

Add list of VisitorFieldValidators

stringRegexs

no

Add list of StringRegexValidator

regexFields

no

Add list of RegexFieldValidator

expressions

no

Add list of ExpressionValidator

...

RequiredStringValidator

This validator checks that a String field is not empty (i.e. non-null with a length > 0).

Parameter

Required

Default

Notes

trim

no

true

Boolean property. Determines whether the String is trimmed before performing the length check.

Example:

...


@RequiredStringValidator(message = "Default message", key = "i18n.key", shortCircuit = true, trim = true)

...

StringLengthFieldValidator

This validator checks that a String field is of the right length. It assumes that the field is a String.

Parameter

Required

Default

Notes

trim

no

true

Boolean property. Determines whether the String is trimmed before performing the length check.

minLength

no

 

Integer property. The minimum length the String must be.

maxLength

no

 

Integer property. The maximum length the String can be.

If neither minLength nor maxLength is set, nothing will be done.

Example:

Code Block
java
java
@StringLengthFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true, trim = true, minLength = "5",  maxLength = "12")

...

StringRegexValidator

This validator checks that a String field matches a configure Regular Expression, if it is not an empty String.

Parameter

Required

Default

Notes

regex

yes

"."

String property. The Regular Expression for which to check a match.

caseSensitive

no

true

Whether the matching of alpha characters in the expression should be done case-sensitively.

Example:

...


@StringRegexValidator(message = "Default message", key = "i18n.key", shortCircuit = true, regex = "a regular expression", caseSensitive = true)

...

EmailValidator

This validator checks that a field is a valid e-mail address if it contains a non-empty String.

Example:

...


@EmailValidator(message = "Default message", key = "i18n.key", shortCircuit = true)

...

UrlValidator

This validator checks that a field is a valid URL.

Example:

...


@UrlValidator(message = "Default message", key = "i18n.key", shortCircuit = true)

...

IntRangeFieldValidator

This validator checks that a numeric field has a value within a specified range.

Parameter

Required

Default

Notes

min

no

 

Integer property. The minimum the number must be.

max

no

 

Integer property. The maximum number can be.

If neither min nor max is set, nothing will be done.

The values for min and max must be inserted as String values so that "0" can be handled as a possible value.

Example:

...


@IntRangeFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true, min = "0", max = "42")

...

DateRangeFieldValidator

This validator checks that a date field has a value within a specified range.

Parameter

Required

Default

Notes

min

no

 

Date property. The minimum the date must be.

max

no

 

Date property. The maximum date can be.

If neither min nor max is set, nothing will be done.

Example:

...


@DateRangeFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true, min = "2005/01/01", max = "2005/12/31")

...

ConversionErrorFieldValidator

This validator checks if there are any conversion errors for a field and applies them if they exist. See Type Conversion Error Handling for details.

Example:

...


@ConversionErrorFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true)

...

ExpressionValidator

This validator uses an OGNL expression to perform its validation. The error message will be added to the action if the expression returns false when it is evaluated against the value stack.

Parameter

Required

Default

Notes

expression

yes

 

An OGNL expression that returns a boolean value.

Example:

...


@ExpressionValidator(message = "Default message", key = "i18n.key", shortCircuit = true, expression = "an OGNL expression" )

...

FieldExpressionValidator

This validator uses an OGNL expression to perform its validation. The error message will be added to the field if the expression returns false when it is evaluated against the value stack.

Parameter

Required

Default

Notes

expression

yes

 

An OGNL expression that returns a boolean value.

Example:

...


@FieldExpressionValidator(message = "Default message", key = "i18n.key", shortCircuit = true, expression = "an OGNL expression")

...

VisitorFieldValidator

The validator allows you to forward validation to object properties of your action using the objects 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.

The error message for the VisitorFieldValidator will be appended in front of validation messages added by the validations for the Object message.

Parameter

Required

Default

Notes

context

no

action alias

Determines the context to use for validating the Object property. If not defined, the context of the Action validation is propogated to the Object property validation. In the case of Action validation, this context is the Action alias.

appendPrefix

no

true

Determines 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. For example, suppose that the bean being validated has a "name" property. If appendPrefix is true, then the field error will be stored under the field "bean.name". If appendPrefix is false, then the field error will be stored under the field "name".
(warning) If you are using the VisitorFieldValidator to validate the model from a ModelDriven Action, you should set appendPrefix to false unless you are using "model.name" to reference the properties on your model.

Example:

...


@VisitorFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true, context = "action alias", appendPrefix = true)

Here we see the context being overridden in the validator mapping, so the action alias context will not be propogated.

ModelDriven example:

...


@VisitorFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true, context = "action alias", appendPrefix = true)

...

   @Validations(
            requiredFields =
                    {@RequiredFieldValidator(type = ValidatorType.SIMPLE, fieldName = "customfield", message = "You must enter a value for field.")},
            requiredStrings =
                    {@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "stringisrequired", message = "You must enter a value for string.")},
            emails =
                    { @EmailValidator(type = ValidatorType.SIMPLE, fieldName = "emailaddress", message = "You must enter a value for email.")},
            urls =
                    { @UrlValidator(type = ValidatorType.SIMPLE, fieldName = "hreflocation", message = "You must enter a value for email.")},
            stringLengthFields =
                    {@StringLengthFieldValidator(type = ValidatorType.SIMPLE, trim = true, minLength="10" , maxLength = "12", fieldName = "needstringlength", message = "You must enter a stringlength.")},
            intRangeFields =
                    { @IntRangeFieldValidator(type = ValidatorType.SIMPLE, fieldName = "intfield", min = "6", max = "10", message = "bar must be between ${min} and ${max}, current value is ${bar}.")},
            dateRangeFields =
                    {@DateRangeFieldValidator(type = ValidatorType.SIMPLE, fieldName = "datefield", min = "-1", max = "99", message = "bar must be between ${min} and ${max}, current value is ${bar}.")},
            expressions = {
                @ExpressionValidator(expression = "foo > 1", message = "Foo must be greater than Bar 1. Foo = ${foo}, Bar = ${bar}."),
                @ExpressionValidator(expression = "foo > 2", message = "Foo must be greater than Bar 2. Foo = ${foo}, Bar = ${bar}."),
                @ExpressionValidator(expression = "foo > 3", message = "Foo must be greater than Bar 3. Foo = ${foo}, Bar = ${bar}."),
                @ExpressionValidator(expression = "foo > 4", message = "Foo must be greater than Bar 4. Foo = ${foo}, Bar = ${bar}."),
                @ExpressionValidator(expression = "foo > 5", message = "Foo must be greater than Bar 5. Foo = ${foo}, Bar = ${bar}.")
    }
    )
    public String execute() throws Exception {
        return SUCCESS;
    }


Anchor
RequiredFieldValidator
RequiredFieldValidator

RequiredFieldValidator

This validator checks that a field is non-null.

Example:

Code Block
java
java

@RequiredFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true)


Anchor
RequiredStringValidator
RequiredStringValidator

RequiredStringValidator

This validator checks that a String field is not empty (i.e. non-null with a length > 0).

Parameter

Required

Default

Notes

trim

no

true

Boolean property. Determines whether the String is trimmed before performing the length check.

Example:

Code Block
java
java

@RequiredStringValidator(message = "Default message", key = "i18n.key", shortCircuit = true, trim = true)


Anchor
StringLengthFieldValidator
StringLengthFieldValidator

StringLengthFieldValidator

This validator checks that a String field is of the right length. It assumes that the field is a String.

Parameter

Required

Default

Notes

trim

no

true

Boolean property. Determines whether the String is trimmed before performing the length check.

minLength

no

 

Integer property. The minimum length the String must be.

maxLength

no

 

Integer property. The maximum length the String can be.

If neither minLength nor maxLength is set, nothing will be done.

Example:

Code Block
java
java

@StringLengthFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true, trim = true, minLength = "5",  maxLength = "12")


Anchor
StringRegexValidator
StringRegexValidator

StringRegexValidator

This validator checks that a String field matches a configure Regular Expression, if it is not an empty String.

Parameter

Required

Default

Notes

regex

yes

"."

String property. The Regular Expression for which to check a match.

caseSensitive

no

true

Whether the matching of alpha characters in the expression should be done case-sensitively.

Example:

Code Block
java
java

@StringRegexValidator(message = "Default message", key = "i18n.key", shortCircuit = true, regex = "a regular expression", caseSensitive = true)


Anchor
EmailValidator
EmailValidator

EmailValidator

This validator checks that a field is a valid e-mail address if it contains a non-empty String.

Example:

Code Block
java
java

@EmailValidator(message = "Default message", key = "i18n.key", shortCircuit = true)


Anchor
UrlValidator
UrlValidator

UrlValidator

This validator checks that a field is a valid URL.

Example:

Code Block
java
java

@UrlValidator(message = "Default message", key = "i18n.key", shortCircuit = true)


Anchor
IntRangeFieldValidator
IntRangeFieldValidator

IntRangeFieldValidator

This validator checks that a numeric field has a value within a specified range.

Parameter

Required

Default

Notes

min

no

 

Integer property. The minimum the number must be.

max

no

 

Integer property. The maximum number can be.

If neither min nor max is set, nothing will be done.

The values for min and max must be inserted as String values so that "0" can be handled as a possible value.

Example:

Code Block
java
java

@IntRangeFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true, min = "0", max = "42")


Anchor
DateRangeFieldValidator
DateRangeFieldValidator

DateRangeFieldValidator

This validator checks that a date field has a value within a specified range.

Parameter

Required

Default

Notes

min

no

 

Date property. The minimum the date must be.

max

no

 

Date property. The maximum date can be.

If neither min nor max is set, nothing will be done.

Example:

Code Block
java
java

@DateRangeFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true, min = "2005/01/01", max = "2005/12/31")


Anchor
ConversionErrorFieldValidator
ConversionErrorFieldValidator

ConversionErrorFieldValidator

This validator checks if there are any conversion errors for a field and applies them if they exist. See Type Conversion Error Handling for details.

Example:

Code Block
java
java

@ConversionErrorFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true)


Anchor
ExpressionValidator
ExpressionValidator

ExpressionValidator

This validator uses an OGNL expression to perform its validation. The error message will be added to the action if the expression returns false when it is evaluated against the value stack.

Parameter

Required

Default

Notes

expression

yes

 

An OGNL expression that returns a boolean value.

Example:

Code Block
java
java

@ExpressionValidator(message = "Default message", key = "i18n.key", shortCircuit = true, expression = "an OGNL expression" )


Anchor
FieldExpressionValidator
FieldExpressionValidator

FieldExpressionValidator

This validator uses an OGNL expression to perform its validation. The error message will be added to the field if the expression returns false when it is evaluated against the value stack.

Parameter

Required

Default

Notes

expression

yes

 

An OGNL expression that returns a boolean value.

Example:

Code Block
java
java

@FieldExpressionValidator(message = "Default message", key = "i18n.key", shortCircuit = true, expression = "an OGNL expression")


Anchor
VisitorFieldValidator
VisitorFieldValidator

VisitorFieldValidator

The validator allows you to forward validation to object properties of your action using the objects 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.

The error message for the VisitorFieldValidator will be appended in front of validation messages added by the validations for the Object message.

Parameter

Required

Default

Notes

context

no

action alias

Determines the context to use for validating the Object property. If not defined, the context of the Action validation is propogated to the Object property validation. In the case of Action validation, this context is the Action alias.

appendPrefix

no

true

Determines 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. For example, suppose that the bean being validated has a "name" property. If appendPrefix is true, then the field error will be stored under the field "bean.name". If appendPrefix is false, then the field error will be stored under the field "name".
(warning) If you are using the VisitorFieldValidator to validate the model from a ModelDriven Action, you should set appendPrefix to false unless you are using "model.name" to reference the properties on your model.

Example:

Code Block
java
java

@VisitorFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true, context = "action alias", appendPrefix = true)

Here we see the context being overridden in the validator mapping, so the action alias context will not be propogated.

ModelDriven example:

Code Block
java
java

@VisitorFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true, context = "action alias", appendPrefix = true)

This will use the model's validation rules and any errors messages will be applied directly (nothing is prefixed because of the empty message).

Complex Examples

An Annotated Interface

  • Mark the interface with @Validation()
  • Apply standard or custom annoations at method level
Code Block
java
java

@Validation()
public interface AnnotationDataAware {

    void setBarObj(Bar b);

    Bar getBarObj();

    @RequiredFieldValidator(message = "You must enter a value for data.")
    @RequiredStringValidator(message = "You must enter a value for data.")
    void setData(String data);

    String getData();
}

An Annotated Class

Code Block
java
java

@Validation()
public class SimpleAnnotationAction extends ActionSupport {

    @RequiredFieldValidator(type = ValidatorType.FIELD, message = "You must enter a value for bar.")
    @IntRangeFieldValidator(type = ValidatorType.FIELD, min = "6", max = "10", message = "bar must be between ${min} and ${max}, current value is ${bar}.")
    public void setBar(int bar) {
        this.bar = bar;
    }

    public int getBar() {
        return bar;
    }

    @Validations(
            requiredFields =
                    {@RequiredFieldValidator(type = ValidatorType.SIMPLE, fieldName = "customfield", message = "You must enter a value for field.")},
            requiredStrings =
                    {@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "stringisrequired", message = "You must enter a value for string.")},
            emails =
                    { @EmailValidator(type = ValidatorType.SIMPLE, fieldName = "emailaddress", message = "You must enter a value for email.")},
            urls =
                    { @UrlValidator(type = ValidatorType.SIMPLE, fieldName = "hreflocation", message = "You must enter a value for email.")},
            stringLengthFields =
                    {@StringLengthFieldValidator(type = ValidatorType.SIMPLE, trim = true, minLength="10" , maxLength = "12", fieldName = "needstringlength", message = "You must enter a stringlength.")},
            intRangeFields =
                    { @IntRangeFieldValidator(type = ValidatorType.SIMPLE, fieldName = "intfield", min = "6", max = "10", message = "bar must be between ${min} and ${max}, current value is ${bar}.")},
            dateRangeFields =
                    {@DateRangeFieldValidator(type = ValidatorType.SIMPLE, fieldName = "datefield", min = "-1", max = "99", message = "bar must be between ${min} and ${max}, current value is ${bar}.")},
            expressions = {
                @ExpressionValidator(expression = "foo > 1", message = "Foo must be greater than Bar 1. Foo = ${foo}, Bar = ${bar}."),
                @ExpressionValidator(expression = "foo > 2", message = "Foo must be greater than Bar 2. Foo = ${foo}, Bar = ${bar}."),
                @ExpressionValidator(expression = "foo > 3", message = "Foo must be greater than Bar 3. Foo = ${foo}, Bar = ${bar}."),
                @ExpressionValidator(expression = "foo > 4", message = "Foo must be greater than Bar 4. Foo = ${foo}, Bar = ${bar}."),
                @ExpressionValidator(expression = "foo > 5", message = "Foo must be greater than Bar 5. Foo = ${foo}, Bar = ${bar}.")
    }
    )
    public String execute() throws Exception {
        return SUCCESS;
    }
}