Este conteúdo não está disponível no idioma selecionado.
1.4. Writing your own constraints
Extending the set of built-in constraints is extremely easy. Any constraint consists of two pieces: the constraint descriptor (the annotation) and the constraint validator (the implementation class). Here is a simple user-defined descriptor:
type is a parameter describing how the property should to be capitalized. This is a user parameter fully dependant on the annotation business.
message is the default string used to describe the constraint violation and is mandatory. You can hard code the string or you can externalize part/all of it through the Java ResourceBundle mechanism. Parameters values are going to be injected inside the message when the {parameter} string is found (in our example Capitalization is not {type} would generate Capitalization is not FIRST ), externalizing the whole string in ValidatorMessages.properties is considered good practice. See Section 1.3, “Error messages” .
As you can see the {} notation is recursive.
To link a descriptor to its validator implementation, we use the
@ValidatorClass meta-annotation. The validator class parameter must name a class which implements Validator<ConstraintAnnotation> .
We now have to implement the validator (ie. the rule checking implementation). A validation implementation can check the value of the a property (by implementing
PropertyConstraint ) and/or can modify the hibernate mapping metadata to express the constraint at the database level (by implementing PersistentClassConstraint )
The
isValid() method should return false if the constraint has been violated. For more examples, refer to the built-in validator implementations.
We only have seen property level validation, but you can write a Bean level validation annotation. Instead of receiving the return instance of a property, the bean itself will be passed to the validator. To activate the validation checking, just annotated the bean itself instead. A small sample can be found in the unit test suite.
If your constraint can be applied multiple times (with different parameters) on the same property or type, you can use the following annotation form:
Basically an annotation containing the value attribute as an array of validator annotations.