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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With