Typical Thymeleaf Rate in Validation Failure Message

I am using validation-api to validate if the input field does not contain invalid characters: <> '"etc. I am using the @Pattern annotation with a custom message translated to something like this: The following <>"' characters are not allowed ". Single quote char is missing when this message is printed. I have the following symptoms:>" "Not allowed.

I tried to use \ 'and \ u0027 and' in message.properties but without success (there is a snippet in the message in the third case). Messages are displayed using:

<p class="error" th:if="${#fields.hasErrors('company.name')}" th:errors="${company.name}">error</p>

      

Thymeleaf version: 2.1.3.RELEASE

Spring version: 3.2.8.RELEASE

Spring webflow version: 2.4.0.RELEASE

+3


source to share


1 answer


I don't know much about Thymeleaft. But keep in mind that you may need to escape single quotes (with two single quotes ( ''

)) if you get the message with MessageFormat

and ResourceBundle

.

If you are using Spring ResourceBundleMessageSource

(which uses internals ResourceBundle

and MessageFormat

) you should do the same.

See the MessageFormat javadoc :



Within a String, a pair of single quotes can be used to quote any arbitrary character other than single quotes. For example, the picture "'{0}'" represents the string "{0}", not FormatElement. The single quote itself must be represented by double single quotes '' throughout the entire Line. For example, the pattern string "'{' '}'" is interpreted as the sequence "{(quote start and left curly brace)" (one quote) and} '(right curly brace and end quote), not' {'and'} '(quoted left and right curly braces): represent the string "{'}", not "{}".

I wrote a blog post some time ago that has more details on this topic: Single quote with Java resource bundles

+9


source







All Articles