Reputation: 12024
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?
Upvotes: 6
Views: 3036
Reputation: 2080
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
Upvotes: 2
Reputation: 19129
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.
Upvotes: 1