Java Reference
In-Depth Information
Required Fields and the on Parameter
You can restrict the required=true validation to a list of event
handlers, such as on={"save", "update"} . Another option is to spec-
ify the event handler(s) for which not to apply the valida-
tion using the ! negation symbol. For example, on="!save" exe-
cutes the required=true validation for every event handler of the
action bean except save ( ). You can also use a list with nega-
tions, as in on={"!save", "!update"} .
Do not mix “positive” and “negative” event handler names in
the on= attribute, such as on={"save", "!update"} , because logi-
cally it doesn't make sense. (Think about it.)
As we can see in the following code, it's very simple to add these vali-
dations with the minlength= and maxlength= attributes:
Download email_06/src/stripesbook/action/ContactFormActionBean.java
@ValidateNestedProperties({
/ * previous validations... * /
@Validate(field="firstName", maxlength=25),
@Validate(field="lastName",
minlength=2, maxlength=40)
})
Since the first and last name fields are optional, each validation is exe-
cuted only if the user enters a value for that field. Now, entering a single
character in the last name field produces the error shown in Figure 4.4 ,
on the next page. Notice that Stripes used the value of minlength= to
make the message more helpful.
As a bonus, Stripes automatically generates the maxlength= attribute in
the form's HTML
<input>
tags to match the value in the maxlength=
attribute of @Validate :
<tr>
<td> First name: </td>
<td><input maxlength="25" type="text" name="contact.firstName"/></td>
</tr>
<tr>
<td> Last name: </td>
<td><input maxlength="40" type="text" name="contact.lastName"/></td>
</tr>
Any decent browser stops accepting characters in the text field after the
maximum length has been reached.
 
 
Search WWH ::




Custom Search