Java Reference
In-Depth Information
Tim Says. . .
Make Judicious Use of ScopedLocalizableError
At first glance, the ScopedLocalizableError class might look rather
complicated. That's because ScopedLocalizableError is designed
exclusively for use in TypeConverters. Of course, like any class,
you can use it elsewhere if you need to—I've just never found it
useful outside of writing TypeConverters.
But when you write your own TypeConverters, you should
always use ScopedLocalizableError , never SimpleError or Localiz-
ableError . Doing so ensures that your custom TypeConverter will
work just like Stripes' built-in ones and make it easy to customize
the error message as appropriate when you use your new con-
verter. At first this might not seem like a big deal—“I'm using
my custom converter on only one page,” you say. Doing it right
from the beginning will mean that when you do need to use
your type converter somewhere else—or better yet someone
else on your team needs to—it'll be ready to go, and there'll
be no temptation to replicate code or do something quick and
dirty.
To get our custom error messages to be part of that mechanism, we
specify the defaultScope and errorName for our type converter in the
constructor of the ScopedLocalizableError class:
Download email_15/src/stripesbook/ext/PhoneNumberTypeConverter.java
errors.add(
new ScopedLocalizableError("converter.phoneNumber", "invalid"));
Now Stripes will search all the different key combinations that we saw
on page 135 . We can define a different error message for a specific form
by using a key that includes the action bean name or field name.
The best part is that the error message we added earlier with the con-
tact.phoneNumber.invalid key still works. Indeed, fieldName.errorName is
among the list of keys that Stripes uses in the search. Since fieldName
is contact.phoneNumber in the contact form and we defined errorName to
be invalid when we created the ScopedLocalizableError , contact.phoneNum-
ber.invalid will match.
 
 
Search WWH ::




Custom Search