In the previous sections, we learned how to use standard JSR-303 Bean Validation annotations to validate fields of our domain object. This works great for simple validations, but sometimes we need to validate some custom rules that aren't available in the standard annotations. For example, what if we need to validate a newly added product's ID, which should not be the same as any existing product ID? To accomplish this type of thing, we can use custom validation annotations.
In this exercise we are going to learn how to create custom validation annotations and how to use them. Let's add a custom product ID validation to our Add new product page to validate duplicate product IDs:.
Create an annotation interface called
ProductId
(ProductId.java
) under the packagecom.packt.webstore.validator
in the source foldersrc/main/java
, and add the following code snippet:package com.packt.webstore.validator...