[jboss-user] [JBoss Seam] - Re: EntityManager in custom validator

shakenbrain do-not-reply at jboss.com
Wed Apr 25 17:43:59 EDT 2007


Turns out that's exactly the way you do it.  I had to make my validator class implement Serializable.  For reference, here's the code:

@Name("prefixValidator")
  | @Validator
  | public class PrefixValidator implements javax.faces.validator.Validator, Serializable {
  | 
  |   @In
  |   EntityManager entityManager;
  | 
  |   @Transactional
  |   public void validate(FacesContext context, UIComponent component, Object value)
  |       throws ValidatorException {
  | 
  |     entityManager.joinTransaction();
  | 
  |     String prefix = (String) value;
  | 
  |     Query q = entityManager.createQuery("from Organization o where o.prefix = :prefix");
  |     q.setParameter("prefix", prefix.toUpperCase());
  |     if (q.getResultList().size() > 0) {
  |       throw new ValidatorException(makeMessage("Prefix is being used already"));
  |     }
  |   }
  | 
  |   private FacesMessage makeMessage(String s) {
  |     FacesMessage message = new FacesMessage();
  |     message.setDetail(s);
  |     message.setSummary(s);
  |     message.setSeverity(FacesMessage.SEVERITY_ERROR);
  |     return message;
  |   }
  | }

      <s:decorate id="prefixDecorator" template="decorateField.xhtml">
  |         <ui:define name="label">Prefix</ui:define>
  |         <h:inputText id="prefix" size="4" value="#{organizationEditor.instance.prefix}" required="true" disabled="#{!organizationEditor.new}">
  |           <f:validator validatorId="prefixValidator"/>
  |           <a:support event="onblur" reRender="prefixDecorator"/>
  |         </h:inputText>
  |       </s:decorate>

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4040763#4040763

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4040763



More information about the jboss-user mailing list