0 Replies Latest reply on Jan 23, 2012 10:20 AM by Andy Peer

    ajaxValidator with custom validator

    Andy Peer Newbie

      Hi, I have a page with this code:

       

       

      <h:inputText id="email_id" value="#{CreateUserManager.email}" styleClass="#{CreateUserManager.emailError}">
          <f:validator validatorId="EmailValidator" />
          <rich:ajaxValidator event="onblur" reRender="email_id, messages" oncomplete="setAnchor('');"                            
              limitToList="true" ignoreDupResponses="true" requestDelay="0" 
              status="status4divCoverAll" />
      </h:inputText>


       

      And this is the validator:

       

       

      public class EmailValidator implements Validator {
      private static Logger logger = Logger.getLogger(EmailValidator.class.getName());
      
      public void validate(FacesContext context, UIComponent component, Object toValidate) {
          logger.info(">>validate");
      
          boolean isValid = true;
          String email = (String)toValidate;
      
          if (null != email) {
              if (!email.equals("")) {
                  isValid = CustomValidators.isMail(email);
              }
          }
      
          if (!isValid) {
              logger.debug("email format is not valid");
              FacesMessage errMsg = Messages.getMessage(PropertiesHelper.getProperty("properties.messages.file"), "emailCheck", null);
              errMsg.setSeverity(FacesMessage.SEVERITY_ERROR);
              component.getAttributes().put("styleClass", "inputErrorClass");
              logger.info("<<validate");
              throw new ValidatorException(errMsg);
          }
          else {
              if (component.getAttributes().get("styleClass") != null) {
                  String restoreStyle = ((String) component.getAttributes().get("styleClass")).replace("inputErrorStyle", "");
                  component.getAttributes().put("styleClass", restoreStyle);
              }
          }
          logger.debug("email format is valid");
          logger.info("<<validate");
      }


       

       

      As you can see, I change the style of the component if it is not valid. The problem is that when the validator fires, messages are correctly rerendered but not the inputText, so the error class is not applied. If I do a normal (not ajax) submit, I see the class, so I think the ajaxValidator tag does not rerender the inputText after validation.