title: Would like to code JSF validator as a method in a base controller class
body: <p>All my entities have a common superclass called <code>AbstractEntity</code>.  </p>
  
  <p>Entities have a <code>name</code> field. The user is not allowed to create an entity with the same name as an existing entity.</p>
  
  <p>For each entity type, there is an associated controller whose methods are referred to from JSF EL.   The controller is where the list of entities is known.</p>
  
  <p>I want to create a validation such that in the input form where the user enters the name of the entity, he gets a validation error if the name is a duplicate.</p>
  
  <pre><code>&lt;p:outputLabel for="name"/&gt;
  &lt;p:inputText id="name" value="#{controlller.selectedEntity.name}"/&gt;
  </code></pre>
  
  <p>Ideally, if the user tries to create a duplicated entity name, the name field would be indicated via same visual presentation one would get for, say, a required field not being filled in:  The input field is highlighted in red, etc.</p>
  
  <p>Much of the markup and controller code is common code (templates, base controller classes, etc.).  I would like to be able to have the "unique name" validation be common code as well as we have lots of entities and I want to be DRY and avoid code bloat.</p>
  
  <p>My dilemma here is that the JSF validation methods I've don't seem to allow me to have a validator in the base controller class.  Validator options are:</p>
  
  <ul>
  <li>Declarative JSR 303 Bean validation - the bean itself doesn't know the list of other entities so that doesn't work.</li>
  <li>Validation use a Method in the Backing bean</li>
  </ul>
  
  <p>These are insufficient because the entity bean doesn't have list of instances of that entity. </p>
  
  <ul>
  <li>Validations which can be expressed via <code>&lt;f:validateXXX&gt;</code> tags</li>
  </ul>
  
  <p>This insufficient because there is no way to express the "unique name" validation requirement.</p>
  
  <ul>
  <li>Imperative Validation </li>
  </ul>
  
  <p>This is insufficient because @FacesValidator s must be classes.  Thus I would have to code a separate  validator class for each entity, inject the controller into that class, etc.   This causes code bloat and inefficiencies.</p>
  
  <p>What (I imagine) I'd like is something like this:</p>
  
  <pre><code>&lt;p:outputLabel for="name"/&gt;
  &lt;p:inputText id="name" value="#{controlller.selectedEntity.name}"&gt;
      &lt;f:validateXXX method=#{controller.validateNameField}/&gt;
  &lt;/p:inputText&gt;
  </code></pre>
  
  <p><code>validateNameField</code> would be a method in the base class for controllers.   </p>
  
  <p>A key observation here is that the validator and the list of existing entities really need to be in the same class.   That's not the case for the entity bean, nor separate <code>@FacesValidator</code> classes.</p>
  
  <p>Is there a way to do this without the code bloat?</p>
  
  <p>Someone flagged this as a duplicate and StackOverflow insists I explain why it is not.  Because the alleged duplicate has to deal with unique constraints in a JPA database.  This has nothing to do with JPA.   </p>