From the course: Java EE: Bean Validation

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Creating custom validation constraints

Creating custom validation constraints

From the course: Java EE: Bean Validation

Start my 1-month free trial

Creating custom validation constraints

- [Instructor] The bean validation API provides another more fine-grained approach to creating your own constraints. You can create your own constraint from scratch, so that's what I'm doing now. Let's have a look at the stock class. Here you can see the code filled this annotate with three constraints. I think we can do better, and convert them into a custom constraint. So the first thing I need is a new annotation. Which I'll call stock code. So I'll just go into here, now create new class, and I'll call it StockCode like so, and of course this is going to be an annotation, so just need to create it does an interface, like so. And now I need to start adding some annotations to this class. So I'm going to start with the target annotation. So, @Target and where do I want to use this annotation? Well let's think, I'm just going to put it on the method, like so, system with this, to make it neater, like so, and I'm going to add the field, and then let's add the annotation type, and we…

Contents