title: Springboot Thymeleaf form validation not working
body: <p>I've trying to validate the fields in Thymeleaf but the errors are not showing in the view page.</p>
  
  <p>My Controller</p>
  
  <pre><code>@Controller
  public class UserController {
  
      private static final String ADD_NEW_USER="user/addUser";
  
      @Autowired
      UserService userService;
          @RequestMapping(value="/user/new", method=RequestMethod.GET)
      public String addUser(Registration register, Model model){
          model.addAttribute("register",register);
          return ADD_NEW_USER;
      }
  
      @RequestMapping(value="/user/new", method=RequestMethod.POST)
      public String addUser(@Valid Registration register, BindingResult result, Model model){
          model.addAttribute("register",register);
          if(result.hasErrors()){
             List&lt;FieldError&gt; err=result.getFieldErrors();
  
             for(FieldError e:err){
                  System.out.println("Error on object ---&gt; "+e.getObjectName()+" on field ---&gt; "+e.getField()+". Message ---&gt; "+e.getDefaultMessage());
             }
             return ADD_NEW_USER;
          }
          return INDEX_PAGE;
      }
  }
  </code></pre>
  
  <p>View template</p>
  
  <pre><code>&lt;form th:action="@{/user/new}" th:method="post" th:object="${register}" id="addUser" role="form"&gt;
      &lt;fieldset&gt;
          &lt;legend&gt; 
              &lt;p&gt;Field with &lt;span class="required"&gt;*&lt;/span&gt; are required&lt;/p&gt;
          &lt;/legend&gt;
          &lt;div class="form-group"&gt;     
              &lt;label for="name"&gt;&lt;span class="required"&gt;* &lt;/span&gt;Name: &lt;/label&gt;
              &lt;input type="text" th:field="*{name}" class="form-control" /&gt; 
              &lt;div th:if="${#fields.hasErrors('name')}" th:errors="*{name}" class="alert alert-danger"&gt;
              &lt;p&gt;Name is invalid&lt;/p&gt;
              &lt;/div&gt;
              &lt;p class="help-block"&gt;Please provide full name for the user&lt;/p&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label for="email"&gt;&lt;span class="required"&gt;* &lt;/span&gt; Email Address: &lt;/label&gt; 
              &lt;input class="form-control" type="email" th:field="*{email}"/&gt;
              &lt;div th:if="${#fields.hasErrors('email')}" th:errors="*{email}" class="alert alert-danger"&gt;
                  &lt;p&gt;Email is invalid&lt;/p&gt;
              &lt;/div&gt;
              &lt;p class="help-block"&gt;Please provide a valid email address. Activation link will be sent to this email&lt;/p&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label for="password"&gt;&lt;span class="required"&gt;* &lt;/span&gt; Password: &lt;/label&gt;
              &lt;input type="password" th:field="*{password}" class="form-control"/&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;input class="btn btn-success" type="submit" name="Register" value="Register"/&gt;
          &lt;/div&gt;
  
      &lt;/fieldset&gt;
  &lt;/form&gt;&lt;!-- ends register form --&gt;
  </code></pre>
  
  <p>Modal</p>
  
  <pre><code>@Entity
  public class Registration {
  
      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      private Long id;
  
      @NotEmpty
      @Size(min=3, max=50)
      @Column(nullable=false)
      private String name;
  
      @Email
      @NotEmpty
      @Column(nullable=false)
      private String email;
  
      private String password;
  
     //getters and setters
  }
  </code></pre>
  
  <p>I can output the console error like this.</p>
  
  <pre><code>Error on object ---&gt; registration on field ---&gt; name. Message ---&gt; size must be between 3 and 50
  Error on object ---&gt; registration on field ---&gt; email. Message ---&gt; may not be empty
  Error on object ---&gt; registration on field ---&gt; name. Message ---&gt; may not be empty
  </code></pre>
  
  <p>I might be missing something but unable to find.</p>