Update CustomConstraintValidator.java
This commit is contained in:
parent
d9abb296b7
commit
a5f0631850
|
@ -1,17 +1,32 @@
|
|||
package com.baeldung.validation.listvalidation.constraint;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
|
||||
public class CustomConstraintValidator implements ConstraintValidator<CustomConstraint, String> {
|
||||
import com.baeldung.validation.listvalidation.model.Movie;
|
||||
|
||||
public class CustomConstraintValidator implements ConstraintValidator<CustomConstraint, List<Movie>> {
|
||||
|
||||
@Override
|
||||
public boolean isValid(String value, ConstraintValidatorContext context) {
|
||||
if (value == null || value.isEmpty()) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
public boolean isValid(List<Movie> values, ConstraintValidatorContext context) {
|
||||
boolean isValid = true;
|
||||
if (values == null || values.isEmpty()) {
|
||||
isValid = false;
|
||||
context.disableDefaultConstraintViolation();
|
||||
context.buildConstraintViolationWithTemplate("Movie list cannot be empty.")
|
||||
.addConstraintViolation();
|
||||
}
|
||||
String regex = "[^A-Za-z0-9].*";
|
||||
for (Movie movie : values) {
|
||||
if (Pattern.matches(regex, movie.getName())) {
|
||||
isValid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return isValid;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue