Hi,
I'm wondering how to handle class level validation when there are two validations that are identical but configured for different fields. Apparently, that is illegal.
The following case: a registration form where users are required to enter an email address and password twice.
The class level validator should compare the values of the the fields and throw an error if they don't match.
The interface looks like this
Code:
@Target({TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = FieldMatchValidator.class)
@Documented
public @interface FieldMatch {
String message() default "error.fieldnomatch";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
String[] value();
}
The value contains an array of fields that are supposed to match.
Now, in order to validate both email and password, I would either want to annotate this FieldMatch validation for both field combinations (which seems to be illegal), or I would want to pass both group of fields ([email, email2],[password,password2]) as a value array (which seems impossible because only 1-dimensional arrays are allowed, or I would want to list multiple values value1=[password,password2],value2=[email,email2]
The latter is possible but less elegant because you have to hardcode the number of fields you can potentially validate nad it is no longer possible to isolate the error to a particular field.
Any ideas on the best approach?
Kind regards,
Marc