What I would like to do is have a standard error message that uses an annotation to fill in a value on the said error message. The code should help explain.
I have a model POJO that has fields on it with JSR-303 annotations as such:
@Min(value = 1)
@Max(value = 9999)
private int someInt;
and PRESENTLY, I have two properties files: one to hold labels for my fields, and another to hold the actual messages:
label.someInt=Some Integer Field
Min="{0}" must be no less than {value}
Then I loop through stuff to build what I want:
for (FieldError a : result.getFieldErrors()) {
boolean found = false;
for (String b : a.getCodes()) {
System.out.println(b);
try {
addErrorMessage(messageSource.getMessage(b,
new Object[] { new DefaultMessageSourceResolvable(
new String[] { "label." + a.getField() }
)},
null));
found = true;
break;
} catch (Exception e){/* nom Exception */}
}
if (!found) {
addErrorMessage(a.getDefaultMessage());
}
}
That's all fine and dandy, but I would like to be able to stick with the annotation-based way of doing things and add some sort of label on the fields. For instance:
@Min(value = 1)
@Max(value = 9999)
@Label(value = "Some Integer Field")
private int someInt;
Then I could write a helper function that would just use the annotation to label the error message I have in the properties file. I also need this to work with Spring-based validation errors such as typeMismatch. Fun stuff, that.
Thanks in advance for your help!