Bean validation - stop validation on first error
Asked Answered
P

2

6

I would like to know if it is possible to stop execution of other validators while one failed?

I have some bean, annotated like this

import org.hibernate.validator.constraints.*;

/*...*/

@NotBlank(message = SignupForm.NOT_BLANK_MESSAGE)
@Email(message = SignupForm.EMAIL_MESSAGE)
private String email;

@NotBlank(message = SignupForm.NOT_BLANK_MESSAGE)
@Length(min = 6, message = SignupForm.PASSWORD_LENGTH_TOO_SHORT_MESSAGE)
private String password;

and heres my JSP

<form:label path="email"><s:message code="email" /></form:label>
<form:errors path="email" element="div" class="text-error"/>
<form:input path="email" class="input-block-level" placeholder="Email address" />

<form:label path="email2"><s:message code="repeat_email" /></form:label>
<form:errors path="email2" element="div" class="text-error"/>
<form:input path="email2" class="input-block-level" placeholder="Email address" />

<form:label path="password"><s:message code="password" /></form:label>
<form:errors path="password" element="div" class="text-error"/>
<form:password path="password" class="input-block-level" placeholder="Password" />

<form:label path="password2"><s:message code="repeat_password" /> </form:label>
<form:errors path="password2" element="div" class="text-error"/>
<form:password path="password2" class="input-block-level" placeholder="Password" />

Now - on my page, when I try to submit empty form I get

E-mail
The value may not be empty!

and

Password
Password must be at least 6 characters long
The value may not be empty!

For password field I get errors from both validators - which is quite undesirable. In case of email field I understand that empty string is a valid email address (?!) and thats why I only get one message at the time.

So is it possible to run validations in specified order and stop processing after first fail or get only first message?

Perri answered 13/3, 2013 at 22:41 Comment(0)
W
2

For the sake of documentation, this is actually for those who try to manually validate their bean using the Validator API and like to stop at the first failure no matter how many validations are defined (and might fail) for that specific field.

Following what Hardy mentioned, when groups are assigned to each validation and @GroupSequence is defined, at the validation time, the validation stops at the first failing group.

Here is a good example for validation grouping https://www.baeldung.com/javax-validation-groups

  • Just make sure to include the bean's class itself as the last group in @GroupSequence
Witcher answered 17/7, 2020 at 23:41 Comment(0)
S
1

The Bean Validation specification defines group sequences to define ordered validation. The idea is that you would define a sequences for password and email and then when you validate the value you need to pass the right group sequence to the validator. However, I don't know myself how one would specify/target the right group in JSF.

To be more specific to your problem, you could use a single @Pattern constraint to express your constraint or you write a custom @Password constraint. In the latter case you could leverage constraint composition and @ReportAsSingleViolation.

Schizont answered 14/3, 2013 at 10:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.