Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Each field constraint should be handled by a distinct validator annotation, or in other words it's not suggested practice to have one field's validation annotation checking against other fields; cross-field validation should be done at the class level. Additionally, the <a href="http://download.oracle.com/otndocs/jcp/bean_validation-1.0-fr-oth-JSpec/" rel="noreferrer">JSR-303 Section 2.2</a> preferred way to express multiple validations of the same type is via a list of annotations. This allows the error message to be specified per match.</p> <p>For example, validating a common form:</p> <pre class="lang-java prettyprint-override"><code>@FieldMatch.List({ @FieldMatch(first = "password", second = "confirmPassword", message = "The password fields must match"), @FieldMatch(first = "email", second = "confirmEmail", message = "The email fields must match") }) 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 Annotation:</p> <pre class="lang-java prettyprint-override"><code>package constraints; import constraints.impl.FieldMatchValidator; import javax.validation.Constraint; import javax.validation.Payload; import java.lang.annotation.Documented; import static java.lang.annotation.ElementType.ANNOTATION_TYPE; import static java.lang.annotation.ElementType.TYPE; import java.lang.annotation.Retention; import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.annotation.Target; /** * Validation annotation to validate that 2 fields have the same value. * An array of fields and their matching confirmation fields can be supplied. * * Example, compare 1 pair of fields: * @FieldMatch(first = "password", second = "confirmPassword", message = "The password fields must match") * * Example, compare more than 1 pair of fields: * @FieldMatch.List({ * @FieldMatch(first = "password", second = "confirmPassword", message = "The password fields must match"), * @FieldMatch(first = "email", second = "confirmEmail", message = "The email fields must match")}) */ @Target({TYPE, ANNOTATION_TYPE}) @Retention(RUNTIME) @Constraint(validatedBy = FieldMatchValidator.class) @Documented public @interface FieldMatch { String message() default "{constraints.fieldmatch}"; Class&lt;?&gt;[] groups() default {}; Class&lt;? extends Payload&gt;[] payload() default {}; /** * @return The first field */ String first(); /** * @return The second field */ String second(); /** * Defines several &lt;code&gt;@FieldMatch&lt;/code&gt; annotations on the same element * * @see FieldMatch */ @Target({TYPE, ANNOTATION_TYPE}) @Retention(RUNTIME) @Documented @interface List { FieldMatch[] value(); } } </code></pre> <p>The Validator:</p> <pre class="lang-java prettyprint-override"><code>package constraints.impl; import constraints.FieldMatch; import org.apache.commons.beanutils.BeanUtils; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; public class FieldMatchValidator implements ConstraintValidator&lt;FieldMatch, Object&gt; { private String firstFieldName; private String secondFieldName; @Override public void initialize(final FieldMatch constraintAnnotation) { firstFieldName = constraintAnnotation.first(); secondFieldName = constraintAnnotation.second(); } @Override public boolean isValid(final Object value, final ConstraintValidatorContext context) { try { final Object firstObj = BeanUtils.getProperty(value, firstFieldName); final Object secondObj = BeanUtils.getProperty(value, secondFieldName); return firstObj == null &amp;&amp; secondObj == null || firstObj != null &amp;&amp; firstObj.equals(secondObj); } catch (final Exception ignore) { // ignore } return true; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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