Custom constraint
Let’s create a custom constraint and have it checked that the first character is an uppercase one.
package me.m1key.jsf.constraints; import static java.lang.annotation.ElementType.ANNOTATION_TYPE; import static java.lang.annotation.ElementType.CONSTRUCTOR; import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.ElementType.PARAMETER; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import javax.validation.Constraint; import javax.validation.Payload; @Documented @Constraint(validatedBy = FirstUpperValidator.class) @Target( { METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) @Retention(RetentionPolicy.RUNTIME) public @interface FirstUpper { String message() default "{me.m1key.jsf.constraints.FirstUpper.message}"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; }
- message - the error message. You can omit curly braces and just hard code it
- groups - this is for grouping validations (if one group fails - other groups are not checked; here we use the default group)
- payload - additional type-safe information might be carried here
Now, we need the validator.
Custom validator
package me.m1key.jsf.constraints; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; public class FirstUpperValidator implements ConstraintValidator<FirstUpper, String> { @Override public void initialize(FirstUpper firstUpper) { // See JSR 303 Section 2.4.1 for sample implementation. } @Override public boolean isValid(String value, ConstraintValidatorContext context) { if (value == null || value.length() == 0) { return true; } return value.substring(0, 1) .equals(value.substring(0, 1).toUpperCase()); } }
Nothing special here. Finally, the usage...
The usage
It’s identical as any other constraint.
@NotNull @Size(min = 3) @FirstUpper public String getName() { return name; }
Summary
I showed you how to write custom constraints and gave a hint about existence and purpose of groups and payloads.
Download source code for this article
Read on
- Great posts on Bean Validation by Gunnar Morling.
- JSR 303
- Getting started with Bean Validation (my post)
Nice tuto guy,
ReplyDeletetks a lot!!!!
Thank you, nice job!! It helps me a lot!!!
ReplyDeleteIt is an excellent blog. Your post is very good and unique. I am eagerly waiting for your new post. Thanks for sharing and keep sharing more good blogs.
ReplyDeleteimpact of social media
applications of artificial intelligence in real world
advantages of php
rpa roles and responsibilities
php technical interview questions
php interview questions and answers for experienced
Valuable blog,Informative content...thanks for sharing, Waiting for the next update…
ReplyDeleteGST Courses in Chennai
Blockchain course in Chennai
Testcomplete online Training
Web Services with SoapUI training online
valuable blog,Informative content...thanks for sharing, Waiting for the next update…
ReplyDeleteIonic Training in Chennai
Ionic Online Course
Phonegap Online Course
Phonegap Online Training
Nice blog! Actually, I am getting more information to read your great post. Thank you. Discover the ultimate resource for Article Submission Sites! Edtech Reader presents a meticulously curated list of high DA PA Article Submission Sites that can turbocharge your content marketing efforts.
ReplyDeleteFor more info visit Article Submission Sites list
This comment has been removed by the author.
ReplyDelete