Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf single quote in validation error message

I'm using validation-api to validate if input field doesn't contain illegal characters: < > ' " etc. I'm using @Pattern annotation with custom message translated to something like this: The following signs < > " ' „ are not allowed. When this message is printed the single quote char is missing. I have: The following signs < > " „ are not allowed.

I've tried using \' and \u0027 and ' in the message.properties but with no success (in third case in the message there is fragment '). 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

like image 844
Piotr Chowaniec Avatar asked Mar 21 '26 07:03

Piotr Chowaniec


1 Answers

I don't know much about Thymeleaft. But be aware that you might need to escape single quotes (with two single quotes ('')) if you retrieve the message using MessageFormat and ResourceBundle.

If you are using Spring's ResourceBundleMessageSource (which uses ResourceBundle and MessageFormat internally) you have to do the same thing.

See MessageFormat javadoc:

Within a String, a pair of single quotes can be used to quote any arbitrary characters except single quotes. For example, pattern string "'{0}'" represents string "{0}", not a FormatElement. A single quote itself must be represented by doubled single quotes '' throughout a String. For example, pattern string "'{''}'" is interpreted as a sequence of '{ (start of quoting and a left curly brace), '' (a single quote), and }' (a right curly brace and end of quoting), not '{' and '}' (quoted left and right curly braces): representing string "{'}", not "{}".

Some time ago I wrote a blog post that provides some more details about exactly this topic: Single quote escaping with Java resource bundles

like image 90
micha Avatar answered Mar 22 '26 19:03

micha