I am new to Spring and am trying to make a validation form with internationalized validation messages. I am using JSR @Valid annotation for my Login Form validation.
I want to perform the below 3 validations:
- Check if username is empty
- Check if password is empty.
- Check if password is valid against username.
I am able to perform all these validations but the problem is that on my UI, I am getting the 3rd validation along with 1st and 2nd validation.
I want to display 3rd validation only when 1st and 2nd validations are passed.
Below is my jsp code :
<label for="username"> <spring:message code="label.email" /> </label>
<form:input path="username" id="username" />
<form:errors path="username" style="color: red" />
<br />
<br />
<label for="password"> <spring:message
code="label.password" /></label>
<form:input path="password" id="password" />
<form:errors path="password" style="color: red" />
<br />
<form:errors path="authenticated" style="color: red" />
My controller code :
@RequestMapping(value = "/login", method = {RequestMethod.POST, RequestMethod.GET})
public String authenticate(@ModelAttribute("user") @Valid User user,BindingResult result, ModelMap model, Locale locale, SessionStatus status, RedirectAttributes redirectAttrs, HttpServletRequest req){
if(result.hasErrors()){
return "home";
}
//code to validate password against username
}
My Bean Code
public class User implements Serializable {
private static final long serialVersionUID = -7788619177798333712L;
@NotEmpty(message = "Please enter your username.")
private String username;
@NotEmpty(message = "Please enter your password.")
private String password;
@AssertTrue(message= "Invalid username or password")
boolean authenticated;
It will be a big help if someone can guide me on this issue.