I use Map for localized values with locale as a key and String as value. For required fields I need to check that at least required locales are set - or at least some value is set. I have implemented validation annotation to be used on such Map fields and a corresponding validator. The problem is, how can I report a missing value? The property path that is used in UI to bind field errors/values, goes wrong each time:
// Domain object:
@LocalizationRequired
private Map<Locale, String> field;
// LocalizationRequiredValidator:
public boolean isValid(Map<Locale, String> map, ConstraintValidatorContext context) {
if (requiredLocales.isEmpty()) {
// Check that there exists any not null value
} else {
context.disableDefaultConstraintViolation();
boolean valid = true;
for (Locale requiredLocale : requiredLocales) {
if (map.get(requiredLocale) == null) { // e.g. fi
valid = false;
context.buildConstraintViolationWithTemplate("LocalizationRequired")
// These end up in wrong property path:
// .addNode(requiredLocale)
// --> field.fi
// .addNode("[" + requiredLocale + "]")
// --> field.[fi]
// .addNode(null).addNode(requiredLocale).inIterable()
// --> field.fi
// .addNode(null).addNode(null).inIterable().atKey(requiredLocale)
// --> field
.addConstraintViolation();
}
}
return valid;
}
}
The correct path for this error is "field[fi]" but it appears I can only access indexed sub properties. In this case the object itself is indexed. I'm using Hibernate Validator.