Java Reference
In-Depth Information
Manual Method Level Validation Invocation
As mentioned previously, method validation is not automatic, and it must be implemented by the provider or
manually invoked via code. The process for invoking method-level validation varies depending upon the type of
validation you want to perform. In all cases, you must use the javax.validation.ExecutableValidator interface
to manually invoke the execution. To obtain a handle on the ExecutableValidator , utilize the Validation.
buildDefaultValidatorFactory.getValidator().forExecutables() call, as demonstrated in the following lines:
ExecutableValidator executableValidator = Validation
Depending upon the type of method-level validation that you want to invoke, make calls to the appropriate
ExecutableValidator methods. Table 6-3 lists the different methods available for invoking validation.
Table 6-3. ExecutableValidator Validation Methods
Method and Description
validateParameters(T object, Method method, Object[] parameterValues, Class<?>... groups)
Validates the arguments (as given in parameterValues ) for the parameters of a given method (identified by method )
validateReturnValue(T object, Method method, Object returnValue, Class<?>... groups)
Validates the return value (specified by returnValue ) of a given method (identified by method )
validateConstructorParameters(Constructor<T> constructor, Object[] parameterValues, Class<?>... groups)
Validates the arguments (as given in parameterValues ) for the parameters of a given constructor (identified by
constructor )
validateConstructorReturnValue(Constructor<T> constructor, T createdObject, Class<?>... groups)
Validates the object (specified by createdObject ) of a given constructor (identified by constructor )
The following example demonstrates how to manually invoke validation. The call will return a Set of
ConstraintViolation objects, one for each violation that occurred. If no issues occurred, then there will be
an empty set returned.
Set<ConstraintViolation> constraintViolations = executableValidator.validateParameters(
widgetOrderingService, placeOrder, new Object[] { null, widget1, 5 })
Group Conversion
Group validation refers to the notion that constraints can belong to one or more groups that will be used for
validation. A new feature of Bean Validation 1.1 is known as group conversion , which I will discuss within this section.
First, we will summarize the concept of validation groups and their associated concepts. After a brief overview,
I'll dig into group conversions.
Applications can elect to validate a specific “group” of constraints based upon an activity that is being performed.
This means that instead of validating all constraints that belong to an object graph, a subset of those constraints can
be validated, if so desired. Why would this be useful? Consider the case where a user is logging into an application
and you want to validate the user name and password. The user object may contain several different constraint
validations, perhaps constraints on e-mail address, street address, and so on, but you need to validate only the
username and password. In such a case, the login process can validate a subset of those constraints by using the
validation group.
Search WWH ::

Custom Search