Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringMVC: How to mark two inputs invalid?

I am using SpringMVC in my webapp to handle forms and their validation.

In my Validator I have code like this:

@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 properly rendered invalid whenn filling the cssErrorClass attribute. Problem now is that the login.error.invalid message is in the error-message list I am going to display twice.

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

like image 589
Ansgar Avatar asked Jan 29 '26 00:01

Ansgar


1 Answers

I suggest to mark all form as invalid in this case:

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

And show it on view by

<form:errors />
like image 62
Slava Semushin Avatar answered Jan 30 '26 12:01

Slava Semushin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!