I have implemented controller with method:
public Page<EventDto> search(@ModelAttribute @Valid EventsCriteria criteria){...}
It works. EventsCriteria
did not have special separate validator for itself, because it has inner annotation checkers like @NotNull
@NotEmpty
. Right now situation is changed and it's not enough to have only this validators. so I've added and registered correspond service to verify some sates.
I want service to verify additional composite cases and single validation like before. But my single inner annotation checkers like @NotNull
@NotEmpty
do not work any more.
Am I configured something incorrectly or it can't work in in same time(specified service and inner checkers)?
My validator example (as it was asked in comments)
There are nothing special, if you know spring, so I missed this part like unimportant to concentrate your attention on my question.
@Component
public class EventsCriteriaValidator implements Validator {
public static final String INVALID_RAGE_PARAMETERS_SPECIFIED = "Invalid rage parameters: please specify " +
"'from' and 'to' or page 'size";
@Override
public boolean supports(Class<?> clazz) {
return clazz.isAssignableFrom(EventsCriteria.class);
}
@Override
public void validate(Object target, Errors errors) {
EventsCriteria toValidate = (EventsCriteria) target;
validateComplexRestriction(toValidate, errors);
}
private void validateComplexRestriction(EventsCriteria toValidate, Errors errors) {
if (toValidate.isRangeEmpty() && toValidate.getSize() == 0) {
errors.rejectValue(SIZE_FIELD, null, INVALID_RAGE_PARAMETERS_SPECIFIED);
}
}
}
Edited:
If you want both custom and default validation you can use
addValidators
ininitBinder
to add your custom validations. This way default validations will not be deleted.