Sometimes there is a need to generate a custom bean validation constraint to suit the needs of an application. In such cases, one must generate a custom validator class, as well as an annotation class for use with the validation class. NetBeans makes it simple to do so, as it provides a wizard that produces skeleton classes that suit the specified criteria for your constraint.
To get started, right-click on the "Source Packages" node of your project or on an existing package, and choose "New"->"Bean Validation"->"Validation Constraint" (Figure 1).
Figure 1: Select "Validation Constraint"
Next, choose a class name for the bean validation annotation class. The name you elect to use will also be used for the annotation. Provide a package name into which you wish to have the bean validation class created. If you wish to have NetBeans automatically generate a validator class, then select the checkbox indicating as such. If you elect to have the validator class generated, then provide a validator class name and a Java type to validate (Figure 2).
Figure 2: Provide Class Name and Options
Once you click finish, the bean validation annotation class will be generated, as well as the validator class, if chosen. You can then customize the class, as needed, to implement the validator.
Figure 3: Generated Bean Validation Annotation Class
Figure 4: Generated Validator Class with Custom Implementation
Once you've implemented the validator, it is ready to use. As seen in Figure 5, simply annotate the field(s) that you wish to have validated.
Figure 5: Utilizing the Custom Bean Validation Class
Recommended Reading on Bean Validation: