Java Reference
In-Depth Information
Pick One Place for @Validate
For a given property, make sure to put the @Validate annotation
on exactly one of either the field, the getter method, or the
setter method. @Validate annotations in multiple places for the
same property will cause a big ol' nasty exception to be thrown
at you.
To make your code easier to read, be consistent. Once you've
chosen where to put validations (say, on the setter method),
use the same place for all properties if possible.
Suppose we have a Person property in an action bean, and suppose the
Person class has an age property. As in the previous example, the age
is a required field with a minimum of eighteen. We can't do this:
// This won't work
public class Person {
@Validate(required= true , minvalue=18)
private int age;
}
Instead, put @Validate inside a @ValidateNestedProperties annotation on
the Person property of the action bean, like this:
public class MyActionBean implements ActionBean {
@ValidateNestedProperties({
@Validate(field="age", required= true , minvalue=18)
})
private Person person;
}
When @Validate is within @ValidateNestedProperties , indicate the field
being validated with the field= attribute. You can add multiple @Validate
annotations to @ValidateNestedProperties , one for each nested property
being validated. Fields can be deeply nested, as in field="hair.color.rgb" ,
so there's no limit to how far into your model you can go when you're
adding validations.
Built-in Validations
Stripes offers several built-in validations with attributes of @Validate :
• Required field
• Length of the input
 
 
Search WWH ::




Custom Search