I have a Spring Boot web application and I reject a value in a controller like this:
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String createSubmit(@ModelAttribute("createForm") CreateForm createForm, BindingResult result, SessionStatus status) {
DateTime dt1 = createForm.getDt1();
DateTime dt2 = createForm.getDt2();
if (!dt1.isBefore(dt2)){
result.rejectValue("fieldId", "validation.isbefore", new Object[]{dt1, dt2}, "first date must be before second");
}
}
so, if date dt1 is not before dt2, the value gets rejected. Now, I have pretty standard ResourceBundleMessageSource with this entry in messages_en.properties:
validation.isbefore = Start date {0} must be before end date {1}
When the validation error occurs though, I get a message Start date 3/21/16 5:01 PM must be before end date 3/20/16 5:01 PM (both dt1 and dt2 uses their toString() to format the message).
Now, java.text.MessageFormat does support some formatting, namely {0,date,short}. But that only works for java.util.Date, not Joda Time (or any other custom classes for that matter).
Is there a way to customize how are error message arguments formatted? I don't want to do it at the time of validation (in final code the validator itself is decoupled from the controller, have no info about selected language, so it does not know what date format to use).