I am unable to catch any validation in html form using thymleaf template.
Form Object -
public class TestForm {
....
@Pattern(regexp = "[a-zA-Z]*", message = "Only characters")
private String field1;
....
getter/setters
}
html code -
<div id="main">
<form action="#" th:action="@{/create}" th:object="${testForm}" method="post" >
<div>
<label> Field1:</label>
<input type="text" th:field="${testForm.field1}" name="field1" />
<div th:if="${#fields.hasErrors('field1')}" th:errors="*{field1}">errors</div>
</div>
<input type="submit" value="Submit" />
</form>
</div>
controller -
@RequestMapping(method= RequestMethod.POST, value = "/create")
public String createTest(@Valid @ModelAttribute("testForm") TestForm testForm, MultipartHttpServletRequest request, BindingResult bindingResult) throws IOException {
if (bindingResult.hasErrors())
return "createtestform";
No error is thrown and form gets submitted successfully.