title: How do I use the Checkbox with Java + Spring in Thymeleaf/HTML?
body: <p>How do I receive either true or false from an checkbox in html with Thymeleaf over to my controller, so I can take the value either true or false and save in my DB. So far I receive these errors: </p>
  
  <ol>
  <li><pre>org.thymeleaf.exceptions.TemplateInputException:
  An error happened during template parsing (template: "class path resource [templates/normal/start-dag.html]")</pre></li>
  <li><pre>Caused by: org.attoparser.ParseException:
  Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputCheckboxFieldTagProcessor' (template: "normal/start-dag" - line 24, col 44)</pre></li>
  <li><pre>Caused by: org.thymeleaf.exceptions.TemplateProcessingException:
  Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputCheckboxFieldTagProcessor' (template: "normal/start-dag" - line 24, col 44)</pre></li>
  <li><pre>2018-07-17 09:05:16.097 ERROR 6713 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed;
  nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/normal/start-dag.html]")] with root cause</pre></li>
  <li><pre>java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'goodNightOfSleep' available as request attribute
  at org.springframework.web.servlet.support.BindStatus.(BindStatus.java:153) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]</pre></li>
  </ol>
  
  <p>My html looks like this:</p>
  
  <pre><code>&lt;table&gt;
      &lt;tr&gt;
          &lt;input type="checkbox" th:path="goodNightOfSleep"&gt;
          &lt;label th:for="${#ids.next('goodNightOfSleep')}" th:text="#{StartDay.goodNightOfSleep}"&gt;Kan du huske hvad du drømte?&lt;/label&gt;
          &lt;input type="checkbox" th:field="*{goodNightOfSleep}"/&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>And my Controller:</p>
  
  <pre><code>// Start Day
  @GetMapping("/normal/start-dag")
  public  String opretGoal() {
      return "normal/start-dag";
  }
  
  @PostMapping("/normal/start-dag")
  public String opretGoal(@ModelAttribute StartDay startDay, BindingResult bindingResult) {
      if (bindingResult.hasErrors()) {
          return "/normal/menu";
      }
      startDayService.createGoalOfTheDay(startDay);
  
      return "normal/menu";
  }
  </code></pre>
  
  <p>My StartDay.java class: </p>
  
  <pre><code>@Entity
  @Table(name = "start_day")
  public class StartDay {
  
      @Id
      @Column(name = "age_in_days", nullable = true)
      private int ageInDays;
      @Column(name = "day_created", nullable = true)
      private String dayCreated;
      @Column(name = "username", nullable = true)
      private String username;
      @Column(name = "dream_remembered", nullable = true)
      private boolean dreamRemembered;
      @Column(name = "nightmare", nullable = true)
      private boolean nightmare;
      @Column(name = "waking_time", nullable = true)
      private int wakingTime;
      @Column(name = "good_night_of_sleep", nullable = true)
      private boolean goodNightOfSleep;
  </code></pre>
  
  <p>Any help appreciated :)</p>
  
  <p>UPDATE #1 </p>
  
  <p>So I just tried to move the second th:field from the html, so it looks like this:</p>
  
  <pre><code>&lt;table&gt;
      &lt;tr&gt;
          &lt;input type="checkbox" th:path="goodNightOfSleep"&gt;
          &lt;label th:for="${#ids.next('goodNightOfSleep')}" th:text="#{StartDay.goodNightOfSleep}"&gt;Kan du huske hvad du drømte?&lt;/label&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>And that makes me actually able to land on the page, but my checkbox looks like this, and doesn't return a value: <a href="https://i.stack.imgur.com/UtzVh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UtzVh.png" alt="enter image description here"></a></p>