Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm surprised this isn't available out of the box. Anyway, here is a possible solution.</p> <p>I've created a class level validator, not the field level as described in the original question.</p> <p>Here is the annotation code:</p> <pre class="lang-java prettyprint-override"><code>package com.moa.podium.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 "{com.moa.podium.util.constraints.matches}"; Class&lt;?&gt;[] groups() default {}; Class&lt;? extends Payload&gt;[] payload() default {}; String field(); String verifyField(); } </code></pre> <p>And the validator itself:</p> <pre class="lang-java prettyprint-override"><code>package com.moa.podium.util.constraints; import org.mvel2.MVEL; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; public class MatchesValidator implements ConstraintValidator&lt;Matches, Object&gt; { private String field; private String verifyField; public void initialize(Matches constraintAnnotation) { this.field = constraintAnnotation.field(); this.verifyField = constraintAnnotation.verifyField(); } public boolean isValid(Object value, ConstraintValidatorContext context) { Object fieldObj = MVEL.getProperty(field, value); Object verifyFieldObj = MVEL.getProperty(verifyField, value); boolean neitherSet = (fieldObj == null) &amp;&amp; (verifyFieldObj == null); if (neitherSet) { return true; } boolean matches = (fieldObj != null) &amp;&amp; fieldObj.equals(verifyFieldObj); if (!matches) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate("message") .addNode(verifyField) .addConstraintViolation(); } return matches; } } </code></pre> <p>Note that I've used MVEL to inspect the properties of the object being validated. This could be replaced with the standard reflection APIs or if it is a specific class you are validating, the accessor methods themselves.</p> <p>The @Matches annotation can then be used used on a bean as follows:</p> <pre class="lang-java prettyprint-override"><code>@Matches(field="pass", verifyField="passRepeat") public class AccountCreateForm { @Size(min=6, max=50) private String pass; private String passRepeat; ... } </code></pre> <p>As a disclaimer, I wrote this in the last 5 minutes, so I probably haven't ironed out all the bugs yet. I'll update the answer if anything goes wrong.</p>
 

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