Update CustomConstraintValidator.java
This commit is contained in:
parent
d9abb296b7
commit
a5f0631850
|
@ -1,17 +1,32 @@
|
||||||
package com.baeldung.validation.listvalidation.constraint;
|
package com.baeldung.validation.listvalidation.constraint;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import javax.validation.ConstraintValidator;
|
import javax.validation.ConstraintValidator;
|
||||||
import javax.validation.ConstraintValidatorContext;
|
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
|
@Override
|
||||||
public boolean isValid(String value, ConstraintValidatorContext context) {
|
public boolean isValid(List<Movie> values, ConstraintValidatorContext context) {
|
||||||
if (value == null || value.isEmpty()) {
|
boolean isValid = true;
|
||||||
return false;
|
if (values == null || values.isEmpty()) {
|
||||||
} else {
|
isValid = false;
|
||||||
return true;
|
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