0 Replies Latest reply on Apr 4, 2011 4:30 PM by Andrew Cheung

    Problem using validator to disable command button

    Andrew Cheung Newbie

      Hi, all. I'm having some problems using a validator to disable a command button. I have a datatable containing rows where for each row there is an input field. Down below there is a "save" button that saves the modifications done to the datatable. When a user enters values in the input field, then "onkeyup", the validator is called. It checks to see if the input field contains valid values (valid values are between 0 and 100). If it is not a valid value, it with display an error message. What I wanted to do is to disable the "Save" button when an invalid value is entered in the input field.

       

      What I found was that even though I set the value of the backing bean correctly in the validator in the "validate" method after checking the value of the input field, this value is empty when I tried to display it on the xhtml page (see the outputText id="aaa" on region.xhtml page).

       

      Any help is appreciated.

       

        -Andrew

       

      What I'm using:

      1.      GateIn portal 3.1.0

      2.      JSF 1.2 (this is the most up-to-date version of JSF that GateInPortal 3.1.0 supports)

      3.      Facelets 1.1.15.b1.jar

      4.      RichFaces 3.3.3Final

      5.      JBOSS Portlet Bridge 2.0.0

       


      Here's the code in various files:

       

      region.xhtml:

      ----------------------

       

          <a4j:form id="addBrokerageForm">

      <rich:dataTable id="dtSelectedBrokerages" value="#{myBean.brokeragesByRegion}" var="sb">

      <h:column>

      <h:outputText value="#{sb.name}"/>

      </h:column>

      <h:column>

      <rich:message styleClass="errMsg" id="errMsg" for="txtWeight">

      </rich:message>

      <h:inputText id="txtWeight" size="3" maxlength="3" value="#{sb.weight}">

      <f:validator validatorId="weightvalidator" />

      <a4j:support event="onkeyup" ajaxListener="weightvalidator" reRender="errMsg,aaa,btnSave"/>

      </h:inputText>

      </h:column>

      </rich:dataTable>

       

       

      <a4j:commandButton  id="btnSave" rendered="#{!ValidationControlledBean.saveButtonDisabled}" action="#{myBean.saveBrokerages}" value="Save" oncomplete="#{rich:component('brokeragessaved')}.show();return false;" reRender="pnlgdSaveResult"></a4j:commandButton>   

      <h:outputText id="aaa" rendered="true" value="xxx#{ValidationControlledBean.saveButtonDisabled}yyy"/>

      </a4j:form>

       

       

      (Note : there are other modalpanel code in this file, but they are irrelevant to this issue since they appear AFTER user clicks on the Save button. Therefore I'm not showing that part of the code in the file.)

       

      ---------------------------------------------------------------

       

       

      WeightValidator.java:

      ----------------------------

       

       

      public class WeightValidator  implements Validator {

       

          private static final Logger myLogger = Logger.getLogger(WeightValidator.class.getName());

       

          public WeightValidator(){}

       

          @Override

          public void validate(FacesContext facesContext, UIComponent uiComponent, Object object)

                  throws ValidatorException {

              // TODO Auto-generated method stub

              String enteredWeight = (String)object;

              myLogger.debug("DEBUG in WeightValidator");

              FacesMessage message = new FacesMessage();

              String tmpMsg = "INVALID INPUT";

       

              try {

                  Integer tmpValue = Integer.valueOf(enteredWeight);

                  myLogger.error("DEBUG : in validate, tmpValue after parsing = " + tmpValue);

                  if (tmpValue < 0 || tmpValue > 100) {

                        message.setSummary(tmpMsg);

                          ValidationControlledBean.setWeightvalue(tmpValue.toString());

                          ValidationControlledBean.setSaveButtonDisabled(true);

                          myLogger.error("DEBUG : invalid value, weight = " + ValidationControlledBean.getWeightvalue() + ";savebuttondisabled = " +ValidationControlledBean.isSaveButtonDisabled() );

       

                      throw new ValidatorException(message);

                  }

                  else {

                      ValidationControlledBean.setSaveButtonDisabled(false);

                      ValidationControlledBean.setWeightvalue(tmpValue.toString());

                      message.setSummary("");

                      myLogger.error("DEBUG : in else weightvalue = " + ValidationControlledBean.getWeightvalue() + ";savebuttondisabled = " +ValidationControlledBean.isSaveButtonDisabled() );

       

                  }

       

                  }

              catch (Exception e) {

                  message.setSummary(tmpMsg);

                  ValidationControlledBean.setWeightvalue(enteredWeight);

                  ValidationControlledBean.setSaveButtonDisabled(true);

                  myLogger.error("DEBUG : in general exception, weight = " + ValidationControlledBean.getWeightvalue() + "; savebuttondisabled = " +ValidationControlledBean.isSaveButtonDisabled() );

       

                  throw new ValidatorException(message);

              }

       

          }

      }

       

      ----------------------------------------------

       

      ValidationControlledBean.java:

      ---------------------------------

      public class ValidationControlledBean {

          private static boolean saveButtonDisabled = false;

          private static String weightvalue = "";

       

          public static String getWeightvalue() {

              return weightvalue;

          }

       

          public static void setWeightvalue(String weightvalue) {

              ValidationControlledBean.weightvalue = weightvalue;

          }

       

          public static boolean isSaveButtonDisabled() {

              return saveButtonDisabled;

          }

       

          public static void setSaveButtonDisabled(boolean saveButtonDisabled) {

              ValidationControlledBean.saveButtonDisabled = saveButtonDisabled;

          }

       

       

       

      }

       

      -------------------------------

       

      faces-config.xml:

       

      -------------------

       

      ...

      <managed-bean>

        <description>MyBean</description>

        <managed-bean-name>myBean</managed-bean-name>

        <managed-bean-class>demo</managed-bean-class>

        <managed-bean-scope>session</managed-bean-scope>

      </managed-bean>

      <validator>

        <validator-id>weightvalidator</validator-id>

        <validator-class>demo</validator-class>

      </validator>

      ...

       

      Note : I have also tried putting ValidationControlledBean in faces-config.xml as a management bean and use it on regions.xhtml, but it doesn't work either.