SpringMVC: How do I mark two inputs invalid?

I use SpringMVC in my webapp to process forms and validate them.

In my validator, I have this code:

@Override
public void validate(final Object target, final Errors errors)
{
    final LoginCommand loginCommand = (LoginCommand) target;
    if (checkCredentials(loginCommand.getLogin(), loginCommand.getPassword()) {
        errors.rejectValue("login", "login.error.invalid");
        errors.rejectValue("password", "login.error.invalid");          
    }
    //..

This way I get the correctly displayed invalid when populating the cssErrorClass attribute. The problem is that the login.error.invalid message is in the list of error messages that I will display twice.

What is the best way to mark two input fields invalid, but there is only one message in the error list? I went through all the SpringMVC / api documentation and did not find a way to reject the field without providing a message.

+3
source share
2 answers

I suggest marking the entire form as invalid in this case:

if (checkCredentials(loginCommand.getLogin(), loginCommand.getPassword()) {
    errors.rejectValue("login.error.invalid");         
}

And show it in sight

<form:errors />
+2
,
<%@ taglib uri="spring.tld" prefix="s"%>
<%@ taglib uri="spring-form.tld" prefix="sf"%>

sf: errors, s: bind. ( , ), :

<s:bind path="loginForm.login">
  <sf:errors/>
</s:bind>

. , , spring mvc.

0

All Articles