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.




      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:





          <a4j:form id="addBrokerageForm">

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


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



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


      <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"/>






      <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"/>




      (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.)









      public class WeightValidator  implements Validator {


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


          public WeightValidator(){}



          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) {




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


                      throw new ValidatorException(message);


                  else {




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





              catch (Exception e) {




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


                  throw new ValidatorException(message);










      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;


























      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.