title: How to display list errors in thymeleaf?
body: <p>Here is a sample skeleton of DTO object.</p>
  
  <pre><code>public class MyDTO
  {
      List&lt;Student&gt; students=new ArrayList&lt;&gt;();
  }
  
  public class Student
  {
      String name;
      Integer age;
      // setter and getter methods
  }
  </code></pre>
  
  <p>Now, the user has a chance to enter a lot of students into the list and any student detail might contain an error. The possible errors are student age being greater than 25, and name containing special characters etc.</p>
  
  <p>For example, <code>students[2].name</code> has a special character and <code>student[4].age</code> > 25, then they are errors. Now, I would like to display the error below those fields and also highlight the corresponding fields.</p>
  
  <pre><code>&lt;form th:field="${myDTO}"&gt;
      &lt;input type="text" th:field="*{students[0].name}" th:errorclass="fieldError"/&gt;
      &lt;span class="error" th:if="${#fields.hasErrors('students[0].name')}" th:errors="*{students[0].name}"&gt;&lt;/span&gt;
      &lt;input type="number" th:field="*{students[0].age}" min="15" max="25" th:errorclass="fieldError"/&gt;
      &lt;span class="error" th:if="${#fields.hasErrors('students[0].age')}" th:errors="*{students[0].age}"&gt;&lt;/span&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>I am confused on what to put in <code>th:field</code> attribute? When I write as above, such type of error is the result</p>
  
  <blockquote>
    <p>Neither BindingResult nor plain target object for bean name
    'students+'['+0+']'' available as request attribute.</p>
  </blockquote>
  
  <p>In my validators, I have such type of code..</p>
  
  <pre><code>    int idx=0;
      for(Student st: students)
      {
          errors.pushNestedPath("students["+idx+"]");
          ValidationUtils.invokeValidator(studentValidator, st, errors);
          errors.popNestedPath();         
          idx++;
      }       
  </code></pre>
  
  <p>and in the <code>StudentValidator</code> class..</p>
  
  <pre><code>@Override
  public void validate(Object obj, Errors errors) {
      Student s=(Student) obj;
      if(containsSpecialCharacters(s.name))
      {
          errors.rejectValue("name","name.containsSpecialCharacters",null,null);
      }
      if(s.age&gt;25 || s.age&lt;15)
      {
          errors.rejectValue("age","age.invalid",null,null);
      }
  }
  </code></pre>
  
  <p>Now, my problems are</p>
  
  <ol>
  <li><p>How do I show those errors, highlight the corresponding fields?</p></li>
  <li><p>What to put in the <code>th:field</code> tag?</p></li>
  <li><p>Next, the student records are added dynamically, that is the student rows doesn't exist previously, by clicking on <strong>Add student</strong> button, the user will be able to add the student. Now, even the <code>th:field</code> must also be updated. How to do that, because it is related to thymeleaf template processing which is done previously but not after the page is loaded?</p></li>
  </ol>
  
  <p>Hope you will reply as soon as possible.</p>