对于嵌套验证,可以执行以下操作:
public class MyFormValidator implements Validator {
private TypeAValidator typeAValidator;
@Override
public boolean supports(Class clazz) {
return MyForm.class.equals(clazz);
}
@Override
public void validate(Object target, Errors errors) {
MyForm myForm = (MyForm) target;
typeAValidator = new TypeAValidator();
int idx = 0;
for (TypeA item : myForm.getListObjects()) {
errors.pushNestedPath("listObjects[" + idx + "]");
ValidationUtils.invokeValidator(this.typeAValidator, item, errors);
errors.popNestedPath();
idx++;
...
}
...
}
}
public class TypeAValidator implements Validator{
@Override
public boolean supports(Class<?> clazz) {
return TypeA.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
TypeA objTypeA = (TypeA)target;
ValidationUtils.rejectIfEmpty(errors, "number", "number.notEmpty");
}
}
希望这有帮助。