カスタム Validator を実装しました。
メッセージは正常に返されますが、propertyPath は返されません。どうすればそれを元に戻すことができるでしょうか?
オブジェクトへの実装
@RequiredIfSet.List({
@RequiredIfSet(propertyPath = "reporterFirstName", field = "isFillingOutForSomeoneElse", dependentField = "reporterFirstName", message = "Reporter First may not be null"),
@RequiredIfSet(propertyPath = "reporterLastName", field = "isFillingOutForSomeoneElse", dependentField = "reporterLastName", message = "Reporter Last may not be null"),
@RequiredIfSet(propertyPath = "reporterContactPhone", field = "isFillingOutForSomeoneElse", dependentField = "reporterContactPhone", message = "Reporter Contact Phone may not be null"),
@RequiredIfSet(propertyPath = "reporterEmail", field = "isFillingOutForSomeoneElse", dependentField = "reporterEmail", message = "Reporter Email may not be null")
})
//制約クラス
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Constraint(validatedBy = RequiredIfSetValidator.class)
public @interface RequiredIfSet {
String field();
String dependentField();
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
String message() default "Field Required";
String propertyPath() default "";
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@interface List {
RequiredIfSet[] value();
}
}
//無効なフィールドをループする場所
List<String[]> invalidFields = new ArrayList<String[]>();
Iterator<ConstraintViolation<T>> iterator = cv.iterator();
while(iterator.hasNext()) {
ConstraintViolation<T> i = iterator.next();
String property = i.getPropertyPath().toString();
String message = i.getMessage();
invalidFields.add(new String[] { property, message });
}
EntityValidationDTO EVDTO = new EntityValidationDTO();
EVDTO.setStatus("fail");
EVDTO.setInvalidFields(invalidFields);
return EVDTO;