1
votes

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>
3

3 Answers

7
votes

Found my reason , i was not passing the correct parameters in my saveForm Controller-

@RequestMapping(value = "/register", method = RequestMethod.POST)
    public String saveForm(@ModelAttribute("registerCommand") @Valid Users users, BindingResult bindingResult, Model model){

        if(bindingResult.hasErrors()) {

            return "registerPage";
        }
        else {
        userRepository.save(users);
        return "register-success";
        }
    }

Note-If you are using BindingResult,it should be exactly after the bean.

It is only BindingResult where the order matters. It must follow the bean that is being validated so that Spring knows where to put any validation errors.

Thanks to David Lavender's answer https://stackoverflow.com/a/29075342/6547517

0
votes

Field 'salary' on object User may be null

0
votes

Check what data you are passing in user object while saving it, there might be salary value is going as null. As you have used @valid on Users , it will not take null or empty value in the input request data