I am having trouble getting my validation message to be resolved.
I have been searching and reading through the web and SO for some hours now, I want to relate the question with the marked answer of Customize spring validation error
I do have a MessageSource
bean defined and the messages.properties it getting read correctly, as I also use it for regular text to be displayed with th:text="#{some.prop.name}
, which does work absolutely fine.
It is just the validation error that won't work the way it should.
I'm sure it's a stupid mistake I just overlook...
The validation itself works fine.
Constraint:
@NotEmpty(message="{validation.mail.notEmpty}")
@Email()
private String mail;
messages.properties:
# Validation
validation.mail.notEmpty=The mail must not be empty!
Template part:
<span th:if="${#fields.hasErrors('mail')}" th:errors="*{mail}"></span>
The displayed text:
{validation.mail.notEmpty}
I tried a lot of variation, all without success.
@NotEmpty(message="validation.mail.notEmpty")
@NotEmpty(message="#{validation.mail.notEmpty}")
Will just show the exact value of the messages string, no parsing.
<span th:if="${#fields.hasErrors('mail')}" th:errors="${mail}"></span>
<span th:if="${#fields.hasErrors('mail')}" th:errors="#{mail}"></span>
<span th:if="${#fields.hasErrors('mail')}" th:errors="#{*{mail}}"></span>
<span th:if="${#fields.hasErrors('mail')}" th:errors="#{__*{mail}__}"></span>
Will result in an error.
EDIT:
After debugging, I stumbled up on this:
Class: org.springframework.context.support.MessageSourceSupport
Method: formatMessage(String msg, Object[] args, Locale locale)
will be called with
formatMessage("{validation.mail.notEmpty}", null, locale /*German Locale*/)
And it will run into if (messageFormat == INVALID_MESSAGE_FORMAT) {
So... my message format is not correct. This is way out of my scope/knowledge. Anyone knows what that means?