I am trying to show validation errors in my form, but I am not able to implement it. I am trying to pass null values, I get the hibernate validation message but I am not able to see form validation?
Here is my code:
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String saveForm(@Valid Users users,Model model,Errors errors) {
if(errors.hasErrors()) {
return "registerPage";
}
else {
model.addAttribute("message","Registered................");
System.out.println("Save Users TEST------------------------------------>");
userRepository.save(users);
return "register-success";
}
}
Here is the exception:
Field error in object 'users' on field 'salary': rejected value [null]; codes [NotNull.users.salary,NotNull.salary,NotNull.java.lang.Integer,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [users.salary,salary]; arguments []; default message [salary]]; default message [must not be null] Field error in object 'users' on field 'dept': rejected value []; codes [Size.users.dept,Size.dept,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [users.dept,dept]; arguments []; default message [dept],10,2]; default message [Length should be in between 2 to 10] Field error in object 'users' on field 'name': rejected value []; codes [Size.users.name,Size.name,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [users.name,name]; arguments []; default message [name],30,2]; default message [Length should be in between 2 to 30]]
Here is my User Pojo:
public class Users implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private Integer id;
@Column(name="name")
@NotNull
@Size(min=2,max=30,message="Length should be in between 2 to 30")
private String name;
@Column(name="salary")
@NotNull
private Integer salary;
@Column(name="dept")
@NotNull
@Size(min=2,max=10,message="Length should be in between 2 to 10")
private String dept;
....
}
Here is my html form(Using thymeleaf):
<h1 th:text="${message}" align="center"></h1>
<div class="container">
<h2>Registration form</h2>
<form class="form-horizontal" th:object="${registerCommand}" th:action="@{/rest/users/register}" method="post">
<div class="form-group">
<label for="name">Username:</label>
<input type="text"
class="form-control" id="name" placeholder="Enter Name"
name="name" th:field="*{name}"> <br />
<p th:if="${#fields.hasErrors('name')}" th:errors="*{name}" th:field="*{name}"
class="alert alert-danger"></p>
</div>
<div class="form-group">
<label for="dept">Department:</label>
<input type="text"
class="form-control" id="dept" placeholder="Enter Department"
name="dept" th:field="*{dept}"> <br />
<p th:if="${#fields.hasErrors('dept')}" th:errors="*{dept}" th:field="*{dept}"
class="alert alert-danger"></p>
</div>
<div class="form-group">
<label for="salary">Salary:</label> <input type="text"
class="form-control" id="salary" placeholder="Enter salary"
name="salary" th:field="*{salary}"> <br />
<p th:if="${#fields.hasErrors('salary')}" th:errors="*{salary}" th:field="*{salary}"
class="alert alert-danger"></p>
</div>
<button type="submit" class="btn btn-sucess">Submit</button>
</form>
</div>