Reputation: 219
I have a problem with my UserValidator class. I'm trying to validate 2 fields of form: username and email and only in case of username the messahe is showing. It's weird for me, because i'm doing it the same way in both cases.
I have globalmessages.properties file with my error messages:
same.mail=Email exists, type other email!
equal.user=Username exists, type other username!
UserValidator class is validated in Controller in that way:
@Autowired
private UserValidator userValidator;
@RequestMapping(value = "/adduser", method = RequestMethod.GET)
public String addUser(@Valid @ModelAttribute(value = "user") User user, BindingResult result) {
userValidator.validate(user, result);
return "signin";
}
In validate method i'm checking if user exists in db and if so error is registered. Then i'm doing the same with email. That's UserValidator's validate method:
@Override
public void validate(Object target, Errors errors) {
User user = (User) target;
//check if user exists
Users users = usersDAOImpl.checkByUsername(user.getUsername());
//check if email exists
Emails email = usersDAOImpl.checkByEmail(user.getEmail());
String emailAddresse = email == null ? "null" : "not null";
System.out.println("From UserValidator class, before rejecting values- email: "
+ emailAddresse);
if (users != null)
{
errors.rejectValue("username", "equal.user");
System.out.println("From UserValidator class - user not null: "
+ users.getUsername());
}
if (email != null)
{
errors.reject("email", "same.mail");
System.out.println("From UserValidator class - email not null: "
+ email.getEmail());
}
}
There's my signin form:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<form:form modelAttribute="user" method="get" action="${pageContext.request.contextPath}/adduser" >
<table align="left">
<tr>
<td>Username</td>
<td><form:input path="username" /></td>
<td><font color="red"><form:errors path="username"></form:errors></font></td>
</tr>
<tr>
<td>Password</td>
<td><form:input type="password" path="password" /></td>
<td><font color="red"><form:errors path="password"></form:errors></font></td>
</tr>
<tr>
<td>Confirm password</td>
<td><form:input type="password" path="confpassword" /></td>
<td><font color="red"><form:errors path="confpassword"></form:errors></font></td>
</tr>
<tr>
<td>E-mail</td>
<td><form:input type="email" path="email" /></td>
<td><font color="red"><form:errors path="email"></form:errors></font></td>
</tr>
<tr>
<td><input type="submit" value="Create user"></td>
</tr>
</table>
</form:form>
</body>
</html>
When i type existing username and submit the form, the error for username from globalmessages.properties is displaying and when i type existing email, error message isn't displaying, although usersDAOImpl.checkByEmail method works fine and i can see "From UserValidator class - email not null: " + email.getEmail() on console.
Upvotes: 0
Views: 3156
Reputation: 16041
The method invocation should be rejectValue
for the email
field too. As reject
sets a global error instead of a field error.
Or if you really want a global error, you need to consult the API docs, where you could find out that the signature of reject
is:
void reject(String errorCode, String defaultMessage);
so, in your case it would look like this:
errors.reject("same.mail", "default message for same email address");
This would register a global error with your string in the property file, you just need to create a field in your markup, where the string should appear to the user.
Upvotes: 1