title: Spring boot + Thymeleaf custom error messages
body: <p>I am using spring boot and thymeleaf for a web application.</p>
  
  <p>I want to customize the validation error messages instead of using the default ones. I found the way to do it is creating a file called messages.properties with the constraints and the message. I also found I can create custom messages based on the class I am targeting and I can also use internationalization methods based on this.</p>
  
  <p>In order to override the default messages I created my own messages.properties file:</p>
  
  <pre><code>Size.message=El tamño no es correcto
  NotNull.message=No puede ser nulo
  Min.message=No cumple con el minimo
  </code></pre>
  
  <p>I set the constraints in my class:</p>
  
  <pre><code>public class Person {
  
      @Size(min=2, max=30)
      private String name;
  
      @NotNull
      @Min(value = 18L)
      private Integer age;
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public Integer getAge() {
          return age;
      }
  
      public void setAge(Integer age) {
          this.age = age;
      }
  }
  </code></pre>
  
  <p>This is my form:</p>
  
  <pre><code>&lt;form action="#" th:action="@{/form}" th:object="${person}" method="post"&gt;
  
      &lt;table&gt;
          &lt;tr&gt;
              &lt;td&gt;Name:&lt;/td&gt;
              &lt;td&gt;&lt;input type="text" th:field="*{name}"/&gt;&lt;/td&gt;
              &lt;td th:if="${#fields.hasErrors('name')}" th:errors="*{name}"&gt;Name error&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Age:&lt;/td&gt;
              &lt;td&gt;&lt;input type="text" th:field="*{age}"/&gt;&lt;/td&gt;
              &lt;td th:if="${#fields.hasErrors('age')}" th:errors="*{age}"&gt;Name error&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;button type="submit"&gt;Enviar&lt;/button&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>And I placed the messages.properties in the resources folder:</p>
  
  <p><a href="https://i.stack.imgur.com/qmmYV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qmmYV.png" alt="enter image description here"></a></p>
  
  <p>According to these links I am doing correctly:</p>
  
  <p><a href="https://stackoverflow.com/questions/20323655/thymeleaf-how-to-use-custom-message-key-in-jsr-303-annotation">Thymeleaf : How to use Custom Message Key in JSR-303 Annotation</a></p>
  
  <p><a href="https://stackoverflow.com/questions/30106659/how-to-include-message-properties-with-thymeleaf">How to include message.properties with thymeleaf</a></p>
  
  <p>But it still not display the custom error messages I wrote.</p>
  
  <p>Any suggestion?
  Thanks in advance for your help.</p>
  
  <p>GitHub with the sample code: <a href="https://MichaelKnight@github.com/MichaelKnight/ValidandoFormularios.git" rel="nofollow noreferrer">https://MichaelKnight@github.com/MichaelKnight/ValidandoFormularios.git</a></p>