-
-
Save xuanyu-h/5218e5b6c75a6d67de75bccc8dc84136 to your computer and use it in GitHub Desktop.
Spring MVC Validation - FAQ: Is it possible to integrate multiple validation string in one annotation?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.luv2code.springdemo.mvc.validation; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
import javax.validation.Constraint; | |
import javax.validation.Payload; | |
@Constraint(validatedBy = CourseCodeConstraintValidator.class) | |
@Target( { ElementType.METHOD, ElementType.FIELD } ) | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface CourseCode { | |
// define default course code | |
public String[] value() default {"LUV"}; | |
// define default error message | |
public String message() default "must start with LUV"; | |
// define default groups | |
public Class<?>[] groups() default {}; | |
// define default payloads | |
public Class<? extends Payload>[] payload() default {}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.luv2code.springdemo.mvc.validation; | |
import javax.validation.ConstraintValidator; | |
import javax.validation.ConstraintValidatorContext; | |
public class CourseCodeConstraintValidator | |
implements ConstraintValidator<CourseCode, String> { | |
private String[] coursePrefixes; | |
@Override | |
public void initialize(CourseCode theCourseCode) { | |
coursePrefixes = theCourseCode.value(); | |
} | |
@Override | |
public boolean isValid(String theCode, | |
ConstraintValidatorContext theConstraintValidatorContext) { | |
boolean result = false; | |
if (theCode != null) { | |
// | |
// loop thru course prefixes | |
// | |
// check to see if code matches any of the course prefixes | |
// | |
for (String tempPrefix : coursePrefixes) { | |
result = theCode.startsWith(tempPrefix); | |
// if we found a match then break out of the loop | |
if (result) { | |
break; | |
} | |
} | |
} | |
else { | |
result = true; | |
} | |
return result; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.luv2code.springdemo.mvc; | |
import javax.validation.constraints.Max; | |
import javax.validation.constraints.Min; | |
import javax.validation.constraints.NotNull; | |
import javax.validation.constraints.Pattern; | |
import javax.validation.constraints.Size; | |
import com.luv2code.springdemo.mvc.validation.CourseCode; | |
public class Customer { | |
private String firstName; | |
@NotNull(message="is required") | |
@Size(min=1, message="is required") | |
private String lastName; | |
@NotNull(message="is required") | |
@Min(value=0, message="must be greater than or equal to zero") | |
@Max(value=10, message="must be less than or equal to 10") | |
private Integer freePasses; | |
@Pattern(regexp="^[a-zA-Z0-9]{5}", message="only 5 chars/digits") | |
private String postalCode; | |
@CourseCode(value={"TOPS", "LUV"}, message="must start with TOPS or LUV") | |
private String courseCode; | |
public String getCourseCode() { | |
return courseCode; | |
} | |
public void setCourseCode(String courseCode) { | |
this.courseCode = courseCode; | |
} | |
public String getPostalCode() { | |
return postalCode; | |
} | |
public void setPostalCode(String postalCode) { | |
this.postalCode = postalCode; | |
} | |
public Integer getFreePasses() { | |
return freePasses; | |
} | |
public void setFreePasses(Integer freePasses) { | |
this.freePasses = freePasses; | |
} | |
public String getFirstName() { | |
return firstName; | |
} | |
public void setFirstName(String firstName) { | |
this.firstName = firstName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment