Note that there are some explanatory texts on larger screens.

plurals
  1. POStrategy to have multiple validations for the 'same' class
    text
    copied!<p>Let's say I have a class <code>Address</code> and it is a child of both <code>Contact</code> and <code>Vendor</code> classes. In the case of <code>Contact</code>, the business only really cares about the Address's <em>City</em> field to be required (Contacts are more about the person's name and phone and their location - City). Whereas, for a <code>Vendor</code>, who may get paper mail from us, we want full address information, so <em>Line1</em>, <em>City</em>, <em>State/Province</em>, <em>Country</em>, <em>PostalCode/Zip</em> are all required. </p> <p>My thought was to create a <code>ContactAddress</code> that uses <code>@NotBlank</code> on the <em>City</em> field, and then a subclass <code>VendorAddress</code> that decorates more fields with <code>@NotBlank</code>. </p> <p>I'm just not certain this is the best approach (and it complicates my domain objects and Hibernate mappings). Does anyone have hints for the right pattern? </p> <p>EDIT: further details</p> <pre><code>public class Contact { private Address address; } public class Vendor { private Address address; } public class Address { private String line1; @NotBlank private String city; private String state; private String country; private String postalCode; } </code></pre> <p><code>Address</code> then, can only have <code>@NotBlank</code> on <em>City</em>, because not all uses of Address require the other fields. </p> <p>I should also say, I want to use these classes in JSF (which does that 'validation for free' thing, in addition to manually invoking it in my service layer). This is a "traditional" JSF/Spring/Hibernate app. </p> <p>Maybe I want to use <a href="http://docs.jboss.org/hibernate/validator/5.0/reference/en-US/html_single/#chapter-groups" rel="nofollow">http://docs.jboss.org/hibernate/validator/5.0/reference/en-US/html_single/#chapter-groups</a> ? Not sure how this interacts with JSF's 'free' validation</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