<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" /> <base href="https://hibernate.atlassian.net" />
<title>Message Title</title>
</head>
<body class="jira" style="color: #333; font-family: Arial, sans-serif; font-size: 14px; line-height: 1.429">
<table id="background-table" cellpadding="0" cellspacing="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; background-color: #f5f5f5; border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt">
<!-- header here -->
<tr>
<td id="header-pattern-container" style="padding: 0px; border-collapse: collapse; padding: 10px 20px">
<table id="header-pattern" cellspacing="0" cellpadding="0" border="0" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt">
<tr>
<td id="header-avatar-image-container" valign="top" style="padding: 0px; border-collapse: collapse; vertical-align: top; width: 32px; padding-right: 8px"> <img id="header-avatar-image" class="image_fix" src="https://secure.gravatar.com/avatar/6db67528c0dfb637ac5d957a22643392?d=mm&s=48" height="32" width="32" border="0" style="border-radius: 3px; vertical-align: top" />
</td>
<td id="header-text-container" valign="middle" style="padding: 0px; border-collapse: collapse; vertical-align: middle; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 1px"> <a class="user-hover" rel="gunnar.morling" id="email_gunnar.morling" href="https://hibernate.atlassian.net/secure/ViewProfile.jspa?name=gunnar.morling" style="color:#6c797f;; color: #3b73af; text-decoration: none">Gunnar Morling</a> <strong>updated</strong> an issue
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td id="email-content-container" style="padding: 0px; border-collapse: collapse; padding: 0 20px">
<table id="email-content-table" cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-spacing: 0; border-collapse: separate">
<tr>
<!-- there needs to be content in the cell for it to render in some clients -->
<td class="email-content-rounded-top mobile-expand" style="padding: 0px; border-collapse: collapse; color: #fff; padding: 0 15px 0 16px; height: 15px; background-color: #fff; border-left: 1px solid #ccc; border-top: 1px solid #ccc; border-right: 1px solid #ccc; border-bottom: 0; border-top-right-radius: 5px; border-top-left-radius: 5px; height: 10px; line-height: 10px; padding: 0 15px 0 16px; mso-line-height-rule: exactly">
</td>
</tr>
<tr>
<td class="email-content-main mobile-expand " style="padding: 0px; border-collapse: collapse; border-left: 1px solid #ccc; border-right: 1px solid #ccc; border-top: 0; border-bottom: 0; padding: 0 15px 0 16px; background-color: #fff">
<table class="page-title-pattern" cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt">
<tr>
<td class="page-title-pattern-first-line " style="padding: 0px; border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; padding-top: 10px"> <a href="https://hibernate.atlassian.net/browse/BVAL" style="color: #3b73af; text-decoration: none">Bean Validation</a> / <a href="https://hibernate.atlassian.net/browse/BVAL-494" style="color: #3b73af; text-decoration: none"><img src="cid:jira-generated-image-avatar-627b9b49-95c6-475e-900f-019b9eafe45a" height="16" width="16" border="0" align="absmiddle" alt="New Feature" style="vertical-align: text-bottom" /></a> <a href="https://hibernate.atlassian.net/browse/BVAL-494" style="color: #3b73af; text-decoration: none">BVAL-494</a>
</td>
</tr>
<tr>
<td style="vertical-align: top;; padding: 0px; border-collapse: collapse; padding-right: 5px; font-size: 20px; line-height: 30px; mso-line-height-rule: exactly" class="page-title-pattern-header-container"> <span class="page-title-pattern-header" style="font-family: Arial, sans-serif; padding: 0; font-size: 20px; line-height: 30px; mso-text-raise: 2px; mso-line-height-rule: exactly; vertical-align: middle"> <a href="https://hibernate.atlassian.net/browse/BVAL-494" style="color: #3b73af; text-decoration: none">An annotation to reference other object annotations...</a> </span>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="email-content-main mobile-expand wrapper-special-margin" style="padding: 0px; border-collapse: collapse; border-left: 1px solid #ccc; border-right: 1px solid #ccc; border-top: 0; border-bottom: 0; padding: 0 15px 0 16px; background-color: #fff; padding-top: 10px; padding-bottom: 5px">
<table class="keyvalue-table" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt">
<tr>
<th style="color: #707070; font: normal 14px/20px Arial, sans-serif; text-align: left; vertical-align: top; padding: 2px 0">
Change By:
</th>
<td style="padding: 0px; border-collapse: collapse; font: normal 14px/20px Arial, sans-serif; padding: 2px 0 2px 5px; vertical-align: top"> <a class="user-hover" rel="gunnar.morling" id="email_gunnar.morling" href="https://hibernate.atlassian.net/secure/ViewProfile.jspa?name=gunnar.morling" style="color:#6c797f;; color: #3b73af; text-decoration: none">Gunnar Morling</a>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="email-content-main mobile-expand issue-description-container" style="padding: 0px; border-collapse: collapse; border-left: 1px solid #ccc; border-right: 1px solid #ccc; border-top: 0; border-bottom: 0; padding: 0 15px 0 16px; background-color: #fff; padding-top: 5px; padding-bottom: 10px">
<table class="text-paragraph-pattern" cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 2px">
<tr>
<td class="text-paragraph-pattern-container mobile-resize-text " style="padding: 0px; border-collapse: collapse; padding: 0 0 10px 0"> <span class="diffcontext">Hello,<br />In large code bases, there may be times where developers use a Facade pattern to get a value from a different object. For example in the UserBean object below, getting the email actually comes from the User class. I'd like to add a @ValidateReference annotation (or something like it) to the Spec that references to the object and field for validation. In the current implementation, I have to validate twice using validator.validateValue(User.class, "email", email, EmailGroup.class) to validate the email is formatted correctly and then validator.validate(userBean) to validate that both fields match. I would prefer to validate once on the email property using validator.validate(userBean, EmailGroup.class) -- with the @ValidateReference addition. <br /><br />I could simply copy and paste the email constraints from User to UserBean but then I'd get complaints of not being DRY (Don't Repeat Yourself). <br /><br />Please correct me if my understanding above is incorrect.<br /><br />Code:<br /></span> <span class="diffaddedchars" style="background-color:#ddfade;"><br />{code}<br /></span> <span class="diffcontext">public class User<br />{<br /> private String name;<br /> private String email;<br /><br /> @Size(min = 3, message = "Name should at least be 3 characters long")<br /> public String getName() {<br /> return name;<br /> }<br /> public void setName(String name) {<br /> this.name = name;<br /> }<br /><br /> @Pattern(regexp = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$", message = "This is not a valid email", groups = EmailGroup.class)<br /> public String getEmail() {<br /> return email;<br /> }<br /> public void setEmail(String email) {<br /> this.email = email;<br /> }<br />}<br /></span> <span class="diffaddedchars" style="background-color:#ddfade;">{code}</span> <span class="diffcontext"><br /><br />Code:<br /></span> <span class="diffaddedchars" style="background-color:#ddfade;"><br />{code}<br /></span> <span class="diffcontext">@FieldsValueMatch(first="user.email", second = "confirmEmail", groups= EmailGroup.class)<br />public class UserBean {<br /><br /> private User user = new User(); // more likely injected in a web form.<br /> private String confirmEmail;<br /><br /> <br /> public String getName() {<br /> return user.getName();<br /> }<br /><br /> public void setName(String name) {<br /> this.user.setName( name);<br /> }<br /><br /> @ValidateReference(refClass=User.class method="email" groups=EmailGroup.class)<br /> public String getEmail() {<br /> return user.getEmail();<br /> }<br /> public void setEmail(String email) {<br /> this.user.setEmail(email);<br /> }<br /><br /> public String getConfirmEmail()<br /> {<br /> return confirmEmail;<br /> }<br /> public void setConfirmEmail(String email)<br /> {<br /> this.confirmEmail = email;<br /> }<br />}<br /></span> <span class="diffaddedchars" style="background-color:#ddfade;">{code}</span> <span class="diffcontext"><br /><br />Code:<br /></span> <span class="diffaddedchars" style="background-color:#ddfade;"><br />{code}<br /></span> <span class="diffcontext">package com.networkfleet.constraints;<br /><br /><br />import javax.validation.Constraint;<br />import javax.validation.Payload;<br />import java.lang.annotation.Documented;<br />import java.lang.annotation.Retention;<br />import java.lang.annotation.Target;<br /><br />import static java.lang.annotation.ElementType.ANNOTATION_TYPE;<br />import static java.lang.annotation.ElementType.TYPE;<br />import static java.lang.annotation.RetentionPolicy.RUNTIME;<br /><br /><br />import com.networkfleet.constraints.FieldsValueMatchValidator;<br /><br />import javax.validation.Constraint;<br />import javax.validation.Payload;<br />import java.lang.annotation.Documented;<br />import static java.lang.annotation.ElementType.ANNOTATION_TYPE;<br />import static java.lang.annotation.ElementType.TYPE;<br />import java.lang.annotation.Retention;<br />import static java.lang.annotation.RetentionPolicy.RUNTIME;<br />import java.lang.annotation.Target;<br /><br />/**<br />* Validation annotation to validate 2 fields that have the same value.<br />* An array of fields and their matching confirmation fields can be supplied.<br />*<br />* Example, compare email fields in an HTML form:<br />* @ValueMatch(first = "password", second = "confirmPassword", message = "The password fields must match")<br />*<br />* Example, compare more than 1 pair of fields:<br />* @ValueMatch.List({<br />* @ValueMatch(first = "password", second = "confirmPassword", message = "The password fields must match"),<br />* @ValueMatch(first = "email", second = "confirmEmail", message = "The email fields must match")})<br />*/<br />@Target({TYPE, ANNOTATION_TYPE})<br />@Retention(RUNTIME)<br />@Constraint(validatedBy = FieldsValueMatchValidator.class)<br />@Documented<br />public @interface FieldsValueMatch<br />{<br /> String message() default "Fields don't match";<br /><br /><br /> Class<?>[] groups() default {};<br /> boolean ignoreCase() default false;<br /><br /> Class<? extends Payload>[] payload() default {};<br /><br /> /**<br /> * @return The first field<br /> */<br /> String first();<br /><br /> /**<br /> * @return The second field<br /> */<br /> String second();<br /><br /> /**<br /> * Defines several <code>@FieldMatch</code> annotations on the same element<br /> *<br /> * @see FieldsValueMatch<br /> */<br /> @Target({TYPE, ANNOTATION_TYPE})<br /> @Retention(RUNTIME)<br /> @Documented<br /> @interface List<br /> {<br /> FieldsValueMatch[] value();<br /> }<br />}<br /></span> <span class="diffaddedchars" style="background-color:#ddfade;">{code}</span> <span class="diffcontext"><br /><br />Code:<br /></span> <span class="diffaddedchars" style="background-color:#ddfade;"><br />{code}<br /></span> <span class="diffcontext">package com.networkfleet.constraints;<br /><br />import org.apache.commons.beanutils.BeanUtils;<br />import javax.validation.ConstraintValidator;<br />import javax.validation.ConstraintValidatorContext;<br /><br />public class FieldsValueMatchValidator implements ConstraintValidator<FieldsValueMatch, Object><br />{<br /> private String firstFieldName;<br /> private String secondFieldName;<br /> private boolean ignoreCase;<br /><br /> @Override<br /> public void initialize(final FieldsValueMatch constraintAnnotation)<br /> {<br /> firstFieldName = constraintAnnotation.first();<br /> secondFieldName = constraintAnnotation.second();<br /> ignoreCase = constraintAnnotation.ignoreCase();<br /> }<br /><br /> @Override<br /> public boolean isValid(final Object value, final ConstraintValidatorContext context)<br /> {<br /> try<br /> {<br /> final String firstObj = BeanUtils.getProperty(value, firstFieldName);<br /> final String secondObj = BeanUtils.getProperty(value, secondFieldName);<br /><br /> if (firstObj == null)<br /> {<br /> return secondObj == null;<br /> }<br /><br /> return (ignoreCase)?firstObj.equalsIgnoreCase(secondObj):firstObj.equals(secondObj);<br /> }<br /> catch (final Exception exception)<br /> {<br /> throw new IllegalArgumentException("Could not compare field("+firstFieldName+") with other field ("+secondFieldName+") because one (or more) of the field name(s) is invalid.", exception);<br /> }<br /> }<br />}<br /></span> <span class="diffaddedchars" style="background-color:#ddfade;">{code}</span>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="email-content-main mobile-expand " style="padding: 0px; border-collapse: collapse; border-left: 1px solid #ccc; border-right: 1px solid #ccc; border-top: 0; border-bottom: 0; padding: 0 15px 0 16px; background-color: #fff">
<table id="actions-pattern" cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 1px">
<tr>
<td id="actions-pattern-container" valign="middle" style="padding: 0px; border-collapse: collapse; padding: 10px 0 10px 24px; vertical-align: middle; padding-left: 0">
<table align="left" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt">
<tr>
<td class="actions-pattern-action-icon-container" style="padding: 0px; border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 0px; vertical-align: middle"> <a href="https://hibernate.atlassian.net/browse/BVAL-494#add-comment" target="_blank" title="Add Comment" style="color: #3b73af; text-decoration: none"> <img class="actions-pattern-action-icon-image" src="cid:jira-generated-image-static-comment-icon-c4f3b58c-8e95-4d00-a968-dd7094a340d3" alt="Add Comment" title="Add Comment" height="16" width="16" border="0" style="vertical-align: middle" /> </a>
</td>
<td class="actions-pattern-action-text-container" style="padding: 0px; border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 4px; padding-left: 5px"> <a href="https://hibernate.atlassian.net/browse/BVAL-494#add-comment" target="_blank" title="Add Comment" style="color: #3b73af; text-decoration: none">Add Comment</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<!-- there needs to be content in the cell for it to render in some clients -->
<tr>
<td class="email-content-rounded-bottom mobile-expand" style="padding: 0px; border-collapse: collapse; color: #fff; padding: 0 15px 0 16px; height: 5px; line-height: 5px; background-color: #fff; border-top: 0; border-left: 1px solid #ccc; border-bottom: 1px solid #ccc; border-right: 1px solid #ccc; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; mso-line-height-rule: exactly">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td id="footer-pattern" style="padding: 0px; border-collapse: collapse; padding: 12px 20px">
<table id="footer-pattern-container" cellspacing="0" cellpadding="0" border="0" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt">
<tr>
<td id="footer-pattern-text" class="mobile-resize-text" width="100%" style="padding: 0px; border-collapse: collapse; color: #999; font-size: 12px; line-height: 18px; font-family: Arial, sans-serif; mso-line-height-rule: exactly; mso-text-raise: 2px">
This message was sent by Atlassian JIRA <span id="footer-build-information">(v7.0.0-OD-05-005#70102-<span title="a6815c26b18096920c1ad23cb89377b66994271f" data-commit-id="a6815c26b18096920c1ad23cb89377b66994271f}">sha1:a6815c2</span>)</span>
</td>
<td id="footer-pattern-logo-desktop-container" valign="top" style="padding: 0px; border-collapse: collapse; padding-left: 20px; vertical-align: top">
<table style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt">
<tr>
<td id="footer-pattern-logo-desktop-padding" style="padding: 0px; border-collapse: collapse; padding-top: 3px"> <img id="footer-pattern-logo-desktop" src="cid:jira-generated-image-static-footer-desktop-logo-310b515b-7b51-4a42-b7c1-10d78539c80a" alt="Atlassian logo" title="Atlassian logo" width="169" height="36" class="image_fix" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>