Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Very nice solution bradhouse. Is there any way to apply the @Matches annotation to more than one field?</p> <p>EDIT: Here's the solution I came up with to answer this question, I modified the Constraint to accept an array instead of a single value:</p> <pre class="lang-java prettyprint-override"><code>@Matches(fields={"password", "email"}, verifyFields={"confirmPassword", "confirmEmail"}) public class UserRegistrationForm { @NotNull @Size(min=8, max=25) private String password; @NotNull @Size(min=8, max=25) private String confirmPassword; @NotNull @Email private String email; @NotNull @Email private String confirmEmail; } </code></pre> <p>The code for the annotation:</p> <pre class="lang-java prettyprint-override"><code>package springapp.util.constraints; import static java.lang.annotation.ElementType.*; import static java.lang.annotation.RetentionPolicy.*; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; import javax.validation.Constraint; import javax.validation.Payload; @Target({TYPE, ANNOTATION_TYPE}) @Retention(RUNTIME) @Constraint(validatedBy = MatchesValidator.class) @Documented public @interface Matches { String message() default "{springapp.util.constraints.matches}"; Class&lt;?&gt;[] groups() default {}; Class&lt;? extends Payload&gt;[] payload() default {}; String[] fields(); String[] verifyFields(); } </code></pre> <p>And the implementation:</p> <pre class="lang-java prettyprint-override"><code>package springapp.util.constraints; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; import org.apache.commons.beanutils.BeanUtils; public class MatchesValidator implements ConstraintValidator&lt;Matches, Object&gt; { private String[] fields; private String[] verifyFields; public void initialize(Matches constraintAnnotation) { fields = constraintAnnotation.fields(); verifyFields = constraintAnnotation.verifyFields(); } public boolean isValid(Object value, ConstraintValidatorContext context) { boolean matches = true; for (int i=0; i&lt;fields.length; i++) { Object fieldObj, verifyFieldObj; try { fieldObj = BeanUtils.getProperty(value, fields[i]); verifyFieldObj = BeanUtils.getProperty(value, verifyFields[i]); } catch (Exception e) { //ignore continue; } boolean neitherSet = (fieldObj == null) &amp;&amp; (verifyFieldObj == null); if (neitherSet) { continue; } boolean tempMatches = (fieldObj != null) &amp;&amp; fieldObj.equals(verifyFieldObj); if (!tempMatches) { addConstraintViolation(context, fields[i]+ " fields do not match", verifyFields[i]); } matches = matches?tempMatches:matches; } return matches; } private void addConstraintViolation(ConstraintValidatorContext context, String message, String field) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate(message).addNode(field).addConstraintViolation(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload