Java Reference
In-Depth Information
form is before the current date. In other words, no unborn people in the
contact list, please!
The key to this validation is that the current date is not a static value.
So, we add a simple method in the action bean to provide it:
Download email_06/src/stripesbook/action/ContactFormActionBean.java
public Date getToday() {
return new Date();
}
Now, using an expression makes it a cinch to validate that the birth
date is in the past:
Download email_06/src/stripesbook/action/ContactFormActionBean.java
@ValidateNestedProperties({
/ * previous validations... * /
@Validate(field="birthDate", expression="${this < today}")
})
In the expression ${this < today} , this refers to the birthDate property, and
today calls getToday ( ) to obtain the current date.
Armed with this validation, submitting the form with a birth date in the
future, such as 2040-01-27 , 2 causes the action bean to return the error
“The value supplied (Fri Jan 27 00:00:00 EST 2040) for field Contact
Birth Date is invalid.”
As you can see, using expressions gives you a concise and effective
way of adding validations that are based on other fields or on values
produced by any helper method.
Using Regular Expression Masks
Another way to validate user input is to use a regular expression mask. 3
To be considered valid, the entire input must match the mask. By plac-
ing the regular expression in the mask= attribute of @Validate , you can
validate patterns that would otherwise require gobs of tedious code.
Consider the “Phone number” field in the contact form. For the sake of
the example, let's say that the phone number should be in the format
used in North America: a three-digit area code, followed by a three-digit
2. I'll be happy, but very surprised, if someone reads this topic after 2040!
3. Refer to the java.util.regex.Pattern Javadocs for the regular expression syntax that
Stripes uses.
 
 
 
Search WWH ::




Custom Search