2 Replies Latest reply: May 14, 2012 1:15 PM by 862919 RSS

    How to create JSF input validator

    935636
      Hi,
      I want to implement input field validator in JSF form:
          //JSF page
           
          <h:panelGroup>Session ID</h:panelGroup>
          <h:panelGroup>
              <h:inputText id="sessionid" value="#{DatabaseController.formMap['sessionid']}" >
                  <f:validateLength minimum="0" maximum="15"/>
                  <f:validator binding="#{ValidatorController.validateMessage}" >
                      <f:attribute name="type" value="sessionid" />
                  </f:validator>
                  <f:ajax event="blur" render="sessionid" />                                          
              </h:inputText>
              <h:message id="sessionidMessage" for="sessionid" />
          </h:panelGroup>
           
          // validator
           package com.DX_57.HM_57;
      
      import java.io.Serializable;
      import javax.enterprise.context.SessionScoped;
      import javax.faces.application.FacesMessage;
      import javax.faces.component.UIComponent;
      import javax.faces.context.FacesContext;
      import javax.faces.validator.Validator;
      import javax.faces.validator.ValidatorException;
      import javax.inject.Named;
      
      @Named("ValidatorController")
      @SessionScoped
      public class FormValidator implements Validator, Serializable {
      
          public FormValidator() {
          }
      
          @Override
          public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
              throw new UnsupportedOperationException("Not supported yet.");
          }
      
          public void validateMessage(FacesContext context, UIComponent component, Object value) throws ValidatorException {
      
              if (context.equals("test")) {
                  throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                          "Session ID is already in use, please choose another.", null));
              }
          }
          
          public void validateUsername(FacesContext context, UIComponent component, Object value) throws ValidatorException {
      
              if (context.equals("test")) {
                  throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                          "Username is already in use, please choose another.", null));
              }
          }
      }
      I have a JSF form with 20 input fields. I have to create validators for them. As for as I understand I have to create for each one of them separate validator into separate Java class. This means that I need to add 20 .java files into my WAR package. Is it possible to reduce the number of the .java files and use one big java validator for all input fields. Something like the above code?

      Best Wishes
      Peter

      p.s I tested the above code - it's not working. Nothing happens.

      Edited by: 932633 on May 8, 2012 3:23 AM

      Edited by: 932633 on May 8, 2012 3:24 AM
        • 1. Re: How to create JSF input validator
          935636
          If you want to implement for example 20 validators how would you implement the code in order to simplify it and reduce the number of the java classes?
          • 2. Re: How to create JSF input validator
            862919
            You don't need to create a java class for each input field.

            Instead, you can create one method for each input, like:
            >
            public void validatePostalCode(FacesContext context, UIComponent component, Object value){
            String postalCode = (String) value;
            // do whatever validation you want to
            throw new ValidatorException(new FacesMessage("Invalid one."));
            }
            >

            But for generic validation like "postal code" you may create it in a separated java class to be able to reuse in many screens.

            If what you want to validate is specific for one screen, you can use methods of your ManagedBean.

            See below:
            [Writing a Method to Perform Validation|http://docs.oracle.com/javaee/5/tutorial/doc/bnavb.html#bnave]

            Edited by: Henriqueso on May 14, 2012 11:14 AM