...
Code Block | ||||
---|---|---|---|---|
| ||||
public class SimpleField extends ActionSupport{ @NotNull() @NotEmpty @Length(max = 3) private String name; ... } |
Use the @AssertValid annotation to validate nested objects, like
Code Block |
---|
public class MemberObject extends ActionSupport {
@AssertValid
private Person person = new Person();
public Person getPerson() {
return person;
}
}
|
XML Configuration
OVal provides support for defining the validation via XML. Validation files must end in "-validation.xml" and the rules to find them, are the same rules used to find the validation XML files used by the regular validation mechanisms (default validation in xwork):
...
In this example, when firstAndLast() is executed, the fields firstName and lastName will be validated. When middle() is executed, only middleName will be validated. When a method is annotated with the Profiles annotation, only the validations in the specified profiles will be performed. If no profile is specified for an action method, all the validations in the class will be evaluated.
Internationalization of messages
...
Code Block | ||
---|---|---|
| ||
notnull.field=${field.name} cannot be null
field.too.long=${field.name} value is too long, allowed length is ${max}
|
and this class:
Code Block | ||
---|---|---|
| ||
public class BookActionextends ActionSupport { @NotNull(message = "notnull.field") @Length(max = 3, message = "field.too.long") private String title; @NotNull(message = "You must enter a valid ISBN") private String isbn; ... } |
...
- "title cannot be null"
- "You must enter a valid ISBN"
- "title value is too long, allowed length is 3"
The current OVal "context" object is pushed into the stack for each validator, so it can be accessed from the property file to build the error message. See the OVal javadoc for more properties available in the FieldContext class.
...