concept validator in category java

appears as: validator, Validator
Play for Java

This is an excerpt from Manning's book Play for Java.

  • Using @ValidateWith and defining your own Validator class
  • Defining a new JSR 303 annotation and defining a new Validator class
  • Define an annotation and reference a Validator class
  • Define the Validator class that is referenced by our annotation
  • Let’s define an @EAN annotation that will be used to make sure our EAN number has exactly 13 digits. We need to declare a new annotation and annotate that with the @javax.validation.Constraint annotation later. The @Constraint annotation references a Validator class that we need to implement next. The following listing shows you how to create the annotation.

    Listing 6.26. Custom JSR-303 EAN annotation

    We now need to define a Validator that will hold the business logic to validate our EAN number. Our validator has to implement the JSR-303 ConstraintValidator interface, and we need to implement the isValid method. The isValid method returns a Boolean that indicates whether the value is valid. The following listing shows the EanValidator implementation.