1
votes

I'm trying to achieve the JSR 303 bean validation in Spring 3.0 by using a simple login use case. The problem is if I submit the form without any value the validation is not happening (i.e the BindingResult method hasErrors() always returning 'false' and printing I'm cool !. Following is the code snippet:

@Controller
public class AnnotatedController {
    @RequestMapping(value = "login")
    public String validateLogin(@Valid LoginForm loginForm, BindingResult result, HttpServletRequest request) {
        if(result.hasErrors())
            System.out.println("Got Errors !");
        else
            System.out.println("I'm cool !");
        return "login";
    }
}

The bean looks like this :

    public class LoginForm {
    @NotEmpty
    private String userName;
    @Size(min=2, max=3)
    private String password;

    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

Finally the view:

<table>
    <form:form action="login.htm" modelAttribute="loginForm">
        <tr>
            <td>User :</td>
            <td><form:input path="userName" /></td>
        </tr>
        <tr>
            <td>Password :</td>
            <td><form:input path="password" /></td>
        </tr>
        <tr><td><input type="submit" value="Ok"> </tr>
    </form:form>
</table>

What am I missing ?

2
Just want to validate - Can you please confirm how you have configured Spring-MVC, I am assuming that you have used <mvc:annotation-driven/> or you may have explicitly specified the HandlerAdapter - if you have explicitly specified the handlerAdapter you may need to specify the validator(JSR 303) also explicitly. - Biju Kunjummen
@BijuKunjummen Yes, I have configured it using <mvc:annotation-driven/>. Is there anything else that I should do here ? - Subhajit Roy
Yes, if you are using mvc:annotation-driven, you don't need to do anything else. Can you please also confirm that you have a JSR 303 implementation in your classpath - say hibernate-validator jar files? - Biju Kunjummen
@BijuKunjummen Yeah, I have hibernate-validator-4.3.0.Final.jar & validation-api-1.0.0.GA.jar present in my Eclipse Web App library. - Subhajit Roy

2 Answers

3
votes

Adding <mvc:annotation-driven/>in servlet context XML fixed the issue for me.

0
votes

you are missing form error tag.use like

<table>
<form:form action="login.htm" commandName="logindetails">
    <tr>
        <td>User :</td>
        <td><form:input path="userName" /></td>
        <td><form:errors path="userName" /></td>
    </tr>
    <tr>
        <td>Password :</td>
        <td><form:input path="password" /></td>
        <td><form:errors path="password"  /></td>
    </tr>
    <tr><td><input type="submit" value="Ok"> </tr>
</form:form>

and also you have to maintain property file with error message.

NotEmpty.logindetails.userName = userName is required!
Range.logindetails.password= password value must be between 2 and 3

Example: Click here