How to perform server side validation using annotations

I am using spring 3.0.5RELEASE in my project.Now my requirement is i want to perform the validation to form.I am new to spring and i dont know how to perform and i googled it.Then i found the solution as using hibernate-validator.jar we can achieve it.Whenever i enter the data into the form that data is saving in one entity class named as SampleCommand and in this class i have 5 fields.Firstly i am trying to apply the validation on one field.So i have given as

@NotNull(message = "The above field must not be blank.")
    private Integer sourceId;

The below is the controller class code

public ModelAndView processForm(@ModelAttribute("reports") @Valid ReportsCommand model, ModelAndView modelAndView,BindingResult result,
            HttpServletRequest request, HttpServletResponse response) throws Exception {


        if (result.hasErrors()) {
           return new ModelAndView(new RedirectView("../pages/report/main.jsp"));
        } 
        else{
           ....
          }

The below is the jsp code only to that field.

<form id="report-generate-form" method="post" action="../../reports/html" target="newWindow">
<div class="form-row" style="">
                        <div class="label"><%=Msg.get(MsgEnum.SOURCE_CONFIG_NAME_LABEL)%><em>*</em>
                        </div>
                        <div class="form-row">

                            <select name="sourceId" id="source-dropdown" class="input-field-select mandatory constrained"

                            <select name="sourceName" id="source-dropdown"
                                class="input-field-select mandatory constrained"

                                constraints='{"fieldLabel":"Source Name","mustSelect":"true"}'>
                                <option value="-1">Select</option>
                            </select>
                        </div>
                        <div class="error-container">
                            <div class="error-img"></div>
                            <label class="error"></label>
                        </div>
                        <form:errors path="sourceId" cssClass="error" />
                    </div>
</form>

If i use like this the application is running with out any exceptions but when i am filling the form i missed that sourceId field and click on the submit button.Then it is not displaying the validation message.Any thing more i have to do to display the message.Sorry for my poor English.

Answers


In you controller the method that is processing the form post method must have @Valid annotation on the field marked as @ModelAttribute and the method must have parameter BindingResult result and then you should check the if condition like below.

if(result.hasError()) {
// return the same view having the form.
} else {
// insert the data in database and return the success view.
}

I also recommend you to display your controller and jsp file here.

Hope this helps you. Cheers.


Few things you must do to validate your form 1. use @valid annotation as Japs mentioned 2. Use <form:errors> on JSP to display errors. Check this link for example.


Need Your Help

Unable to Launch Windows Phone Emulator

c# .net unit-testing windows-phone-8 windows-phone-8-emulator

I am trying to run unit tests on our CI server. When i run the following command it tries to launch emulator but fails with the following message