Expert Spring MVC and Web Flow

(Dana P.) #1
brackets—and we call the forPropertymethod and pass firstNameas property name. Next we
call the hasErrorCodemethod and pass in the error code we expect to be present in the Errors
instance. If this error code cannot be found for the firstNameproperty, an exception will be
thrown. This notation is repeated for the lastNameproperty. The hasErrorCodecan be called
more than once for a property and can also be used to check for global errors.
Lastly the otherwiseand noErrors()methods are called. These methods verify if no other
errors than the one we expect are present in the Errorsinstance. The ErrorsVerifierclass is
very convenient to quickly test if your Errorsinstance contains the correct error codes after
being passed to a Validator. Imagine the Java code you would have to write otherwise to test
that the content of the Errorsinstance is as expected!

Summary


Spring offers an excellent validation framework. The Validatorand Errorsinterface form the
backbone and allow you to write your own Validatorimplementations. If you’d rather use
declarative validation, have a look at Valang. This framework creates a Spring Validator
instance based on a set of constraints defined in its proper validation language. Valang is very
convenient to quickly write constraints for your command classes. Its operators, functions,
and date parser functionalities offer more power and flexibility than Validators written in
Java. Whether you write your Validators in Java or Valang, you should always test them to
check whether all error conditions are rejected properly.
Next to validation Spring also offers support for internationalization through its
MessageSourceinterface. This interface will resolve messages for the locale of your users so
that you can offer your applications in their languages.

282 CHAPTER 9 ■VALIDATION

Free download pdf