Validation in Spring 3.x using @Valid annotation:
Below is snippet from the messages_en.properties. I have a form having Username and Password field. When user does not enter anything in Username field, it displays both these messages one below other.
NotEmpty.loginBean.username=Username cannot be Empty
Size.loginBean.username=Size must between 5 to 50 characters.
Any HTML tag given in the message.properties is not interpreted.
NotEmpty.loginBean.username=<li>Username cannot be Empty</li>
Above would display <li> as it is.
Questions:
1) Is there any ways to interpret HTML tag and display its output?
2) Can i show single message though both validation fails?
Ad. 1) Yes, use
htmlEscape="false":Ad. 2) This is actually JSR303’s Achilles’ heel – it can be done, but is neither easy nor clean (see this issue). Order of validating each annotated field is undefined, so trick is to use
@GroupSequenceand custom groups like described here or here.Alternative solution would be to use custom annotation with @ReportAsSingleViolation, but it will not distinct NotEmpty and Size errors as it’ll have its own error message.