java - Spring Boot validation message is not being resolved -


i having trouble getting validation message resolved.

i have been searching , reading through web , hours now, want relate question marked answer of customize spring validation error

i have messagesource bean defined , messages.properties getting read correctly, use regular text displayed th:text="#{some.prop.name}, work absolutely fine. validation error won't work way should. i'm sure it's stupid mistake overlook... validation works fine.

constraint:

@notempty(message="{validation.mail.notempty}") @email() private string mail; 

messages.properties:

# validation validation.mail.notempty=the mail must not empty! 

template part:

<span th:if="${#fields.haserrors('mail')}" th:errors="*{mail}"></span> 

the displayed text:

{validation.mail.notempty} 

i tried lot of variation, without success.

@notempty(message="validation.mail.notempty") @notempty(message="#{validation.mail.notempty}") 

will show exact value of 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 error.


edit:

after debugging, stumbled on this:

class: org.springframework.context.support.messagesourcesupport

method: formatmessage(string msg, object[] args, locale locale)

will called with

formatmessage("{validation.mail.notempty}", null, locale /*german locale*/)

and run if (messageformat == invalid_message_format) {

so... message format not correct. way out of scope/knowledge. knows means?

it looks missing localvalidatorfactorybean definition in application configuration. below can find example of application class defines 2 beans: localvalidatorfactorybean , messagesource uses messages.properties file.

import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.context.messagesource; import org.springframework.context.annotation.bean; import org.springframework.context.support.reloadableresourcebundlemessagesource; import org.springframework.validation.beanvalidation.localvalidatorfactorybean;  @springbootapplication public class application {      @bean     public messagesource messagesource() {         reloadableresourcebundlemessagesource messagesource = new reloadableresourcebundlemessagesource();         messagesource.setbasename("classpath:messages");         messagesource.setdefaultencoding("utf-8");         return messagesource;     }      @bean     public localvalidatorfactorybean validator() {         localvalidatorfactorybean bean = new localvalidatorfactorybean();         bean.setvalidationmessagesource(messagesource());         return bean;     }      public static void main(string[] args) {         springapplication.run(application.class, args);     } } 

having localvalidatorfactorybean bean defined can use custom validation message like:

@notempty(message = "{validation.mail.notempty}") @email private string email; 

and messages.properties:

validation.mail.notempty=e-mail cannot empty! 

and thymeleaf template file with:

<p th:if="${#fields.haserrors('email')}" th:errors="*{email}">name error</p> 

sample application

https://github.com/wololock/stackoverflow-answers/tree/master/45692179

i have prepared sample spring boot application reflects problem. feel free clone , run locally. display translated validation message if value posted form not meet @notempty , @email validation.

webmvcconfigureradapter configuration

in case of extending webmvcconfigureradapter have provide validator overriding getvalidator() method parent class, e.g.:

import org.springframework.context.messagesource; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.context.support.reloadableresourcebundlemessagesource; import org.springframework.validation.validator; import org.springframework.validation.beanvalidation.localvalidatorfactorybean; import org.springframework.web.servlet.config.annotation.enablewebmvc; import org.springframework.web.servlet.config.annotation.webmvcconfigureradapter;  @configuration @enablewebmvc public class webconfiguration extends webmvcconfigureradapter {      @bean     public messagesource messagesource() {         reloadableresourcebundlemessagesource messagesource = new reloadableresourcebundlemessagesource();         messagesource.setbasename("classpath:messages");         messagesource.setdefaultencoding("utf-8");         return messagesource;     }      @bean     @override     public validator getvalidator() {         localvalidatorfactorybean bean = new localvalidatorfactorybean();         bean.setvalidationmessagesource(messagesource());         return bean;     }      // other methods... } 

otherwise if define localvalidatorfactorybean bean in other place overridden , there no effect.

i hope helps.


Comments