Nick King
Nick King

Reputation: 310

Spring annotations : form validation of a bean internal object attribute using thymeleaf

is there a way in Thymeleaf to validate an attribute in object property of a bean? Consider that we do have a Departement class as below :

public class Departement {
   @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY)
   private Long idDept;

   @NotEmpty
   private String name;
}

And another Employee class as follow

public class Employee{
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private Long idEmp;

  @NotEmpty
  @Size(min = 5, message="At least five characters needed")
  private String employeeName;

  @NotNull
  private Departement departement;
}

With the code above using thymeleaf in the employee form only 'employeeName' will be validated by spring because of annotations. Let's take a look here In my controller

@GetMapping( value = "/emp" )
public String save(Model model){
  Employee emp = new Employee();
  emp.setDepartement(new Departement());
  model.addAttribute('employee', emp);
  return 'view';
}
//------------- Form in PostMapping
@PostMapping( value = "/save", @Valid Emp emp, BindingResult bindingResult )
public String savePost(Model model){
if( ! bindingResult.hasErrors() )
    {
 /* Even if departement has not been choosen, my code always goes here 
and print "Form Ok. Departement : 0" instead of reaching the 'else' block, but if departement choosen, 
it prints the correct value of departemnt 
*/
      System.out.println( "Form Ok.\n Departement : " + emp.getDepartement().getIdDept() );
  }else{
           System.out.println( "Missing attributes." );
  }

  return 'view';
}

And here goes the employee form

 <form th:action="@{save}" th:object="${emp}" th:method="POST" >
  <span th:if="${#fields.hasErrors('employeeName') }"th:errors="*{employeeName}"></span>
    <input th:field="*{employeeName}" th:value="${employeeName}" />
//--------
   <div th:object="${emp.departement}">
      <span th:if="${#fields.hasErrors('idDept') }"th:errors="*{idDept}"></span>
      <input th:field="*{idDept}" th:value="${idDept}" />
   </div>
</form>

**Here is my question : How can I validate the employee departement identifier (idDept field) without using javacript in the emplpoyee form? **

NB : I don't use drowpdownlist for displaying departements but prefer an autocomplete field and a hidden field that take the choosen departement id.

Upvotes: 2

Views: 2553

Answers (1)

Stefan Haberl
Stefan Haberl

Reputation: 10559

JSR-303 mandates the use of a @Valid annotation to recursively validate nested components as mentioned in the Hibernate Validator docs.

So just put @Valid on your nested components, in your case on the department field within the employee class:

public class Employee {
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private Long idEmp;

  @NotEmpty
  @Size(min = 5, message="At least five characters needed")
  private String employeeName;

  @NotNull
  @Valid 
  private Departement departement;
}

Upvotes: 6

Related Questions