Java Reference
In-Depth Information
<br/>
<h:commandButton action="#{usersBean.createUser()}"
value="Create User"/>
</h:form>...
If the form is entered with incorrect values, then error messages are displayed to the user. Figure 6-1 shows what
a user may see if invalid values are entered.
Figure 6-1. Validation errors on input
Review: Writing a Custom Constraint Annotation
To create a custom constraint annotation, simply create a Java interface that contains the customized constraint code.
In the following code example, a custom constraint annotation is developed that will provide the same level of validation
as the method validator and bean validation annotation used in the previous section for validating a userid field:
@Pattern(regexp = "[0-9]{7}[JJ]")
@Documented
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface UserIdValidator {
String message() default "{invalid.userid}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
To apply this custom constraint annotation to a field, indicate the interface name after the annotation character,
as follows:
@UserIdValidator
private String userId;
 
Search WWH ::




Custom Search