Java Reference
In-Depth Information
6.4
Creating Messages for Custom Errors
vert an input
String
to a
PhoneNumber
. If the input was not valid, we
created an error. The error message was directly in the code:
errors.add(
new
SimpleError("{1} is not a valid {0}"));
With all other error messages in the
StripesResources.properties
file, it'd be
a shame not to have messages for custom errors in there as well. Say
we wanted to use the following key:
contact.phoneNumber.invalid
To use the message defined with this key, we can simply use the
Local-
izableError
class. This class allows us to specify a resource bundle key
instead of a hard-coded message. For example:
errors.add(
new
LocalizableError("contact.phoneNumber.invalid");
Now we can define the error message in
StripesResources.properties
with
all the other error messages:
contact.phoneNumber.invalid=The phone number is not valid.
That's fine when you are creating errors that are specific to our appli-
cation. But
PhoneNumberTypeConverter
is useful for any phone number
field, not just the one in the contact form. In fact, word of your phone
number type converter has spread around the office, and people from
other departments have asked us whether they could use it in their
applications.
The problem right now is that we're limited to the same error message
for every phone number field. What if other developers use
PhoneNum-
berTypeConverter
in more than one form and need different error mes-
sages in each form?
The
ScopedLocalizableError
class is specifically designed for use in type
converters such as
PhoneNumberTypeConverter
. It takes advantage of
the key lookup mechanism, just like the type converters provided by
Stripes.
Search WWH ::
Custom Search