Java Reference
In-Depth Information
for which event handlers the validations are executed. For example, an
Age field might be required for the buyBeer ( ) event handler, but not for
buyMilk ( ).
One way to control validations is by specifying, in the on= attribute,
the event handlers for which to enforce required= "true", such as @Val-
idate(required="true", on="buyBeer") for the age property. The other way
is to turn off all validations for an event handler by annotating it with
@DontValidate . You'd want to do this for an event handler that doesn't do
anything with the input, such as a cancel ( ) method that just sends the
user back to the previous page. With @DontValidate , the user is allowed
to enter garbage in the fields and then cancel the form.
The subtleties of how on= and @DontValidate work have tripped up many
Stripes users, including myself when I first started using the frame-
work. But it's really not complicated; I have summed it up into two
rules. Read them carefully, and you'll understand how Stripes decides
whether to execute the validations for a given field:
• Rule #1: If the user has filled in a value for the field, it is validated
regardless of the required= and on= attributes.
• Rule #2: If the user has left the field blank, the only validation that
is executed, if present, is required= true.
The idea behind these rules is simple. If the user has entered a value for
a field, it must be validated to prevent invalid values from corrupting
the model. On the other hand, the user shouldn't be scolded for not
filling out an optional field. So, the only possible error for a blank field
is that the field is required.
Understanding this rationale is important to control the execution of
validations.
For
example,
from
the
previous
ContactFormActionBean
chapter had three event handlers:
form ( ), which is called when the user arrives at the form
save ( ), which handles the form submission and saves the contact
information
cancel ( ), which is also a form submission but for which no infor-
mation is saved.
After adding validations in ContactFormActionBean , you need to con-
trol validation execution. For form ( ), the user is just arriving at the
form, and no values have been entered yet. So, all fields are blank.
The optional fields will not be validated, but the required fields will. It
 
 
Search WWH ::




Custom Search