AnsweredAssumed Answered

Transition buttons enabled/disabled

Question asked by compaqnx7300 on Mar 31, 2009
If I do not fulfill mandatory properties in workflow task I can not use "Save changes" button, but I can use transition buttons, what is wrong in my opinion. If mandatory properties are not fulfilled, than transition buttons should be disabled.
I modified renderValidationScript function in UIPropertySheet class as shown below.
If you want, feel free to use it in your source codes:

   private void renderValidationScript(FacesContext context) throws IOException
   {
      ResponseWriter out = context.getResponseWriter();
      UIForm form = Utils.getParentForm(context, this);
        
      // TODO: We need to encode all the JavaScript functions here
      //       with the client id of the property sheet so that we
      //       can potentially add more than one property sheet to
      //       page and have validation function correctly.
     
      // output the validation.js script
      out.write("\n<script type='text/javascript' src='");
      out.write(context.getExternalContext().getRequestContextPath());
      out.write("/scripts/validation.js");
      out.write("'></script>\n<script type='text/javascript'>\n");
     
      // output variable to hold flag for which submit button was pressed
      out.write("var finishButtonPressed = false;\n");
      out.write("var nextButtonPressed = false;\n");
     
      // output the validate() function
      out.write("function validate()\n{\n   var result = true;\n   ");
      out.write("if ((finishButtonPressed || nextButtonPressed) && (");
     
      int numberValidations = this.validations.size();
      List<ClientValidation> realTimeValidations =
         new ArrayList<ClientValidation>(numberValidations);
     
      for (int x = 0; x < numberValidations; x++)
      {
         ClientValidation validation = this.validations.get(x);
        
         if (validation.RealTimeChecking)
         {
            realTimeValidations.add(validation);
         }
        
         renderValidationMethod(out, validation, (x == (numberValidations-1)), true);
      }
     
      // return false if validation failed to stop the form submitting
      out.write(")\n   { result = false; }\n\n");
      out.write("   finishButtonPressed = false;\n   nextButtonPressed = false;\n");
      out.write("   return result;\n}\n\n");
     
      // output the processButtonState() function (if necessary)
      int numberRealTimeValidations = realTimeValidations.size();
      if (numberRealTimeValidations > 0)
      {
         //MODIFICATION: Function gets transitions buttons and disable them if save changes button is disabled (if mandatory variables are not filled)
         String prefix = form.getClientId(context)+NamingContainer.SEPARATOR_CHAR;
         String transition = "transition_";
         int   offset = prefix.length()+transition.length();

         out.write("function getTransitionsButtons() {\n"+
               "var transitionButtons = new Array();\n"+
               "var els = document.getElementsByTagName('*');\n"+
               "var elsLen = els.length;\n"+
               "for (i = 0, j = 0; i < elsLen; i++) {\n"+
                "   var element = els[i];\n"+
               "   if ( (element.id.length>"+offset+") && (element.id.substring(0,"+offset+")=='"+prefix+transition+"') ) {\n"+
               "      transitionButtons[j] = els[i];\n"+
               "      j++;\n"+
               "   }\n"+
               "}\n"+
               "return transitionButtons;\n"+
                  "}\n");
         //-MODIFICATION
         out.write("function processButtonState()\n{\n " +
              "var myButtons = getTransitionsButtons('*');\n"+//MODIFICATION
               "   if (");
        
         for (int x = 0; x < numberRealTimeValidations; x++)
         {
            renderValidationMethod(out, realTimeValidations.get(x),
                  (x == (numberRealTimeValidations-1)), false);
         }
     
         // disable the finish button if validation failed and
         // also the next button if it is present
        
        
         out.write("\n   {\n      document.getElementById('");
         out.write(form.getClientId(context));
         out.write(NamingContainer.SEPARATOR_CHAR);
         out.write(getFinishButtonId());
         out.write("').disabled = true; \n");
         if (this.nextButtonId != null && this.nextButtonId.length() > 0)
         {
            out.write("      document.getElementById('");
            out.write(form.getClientId(context));
            out.write(NamingContainer.SEPARATOR_CHAR);
            out.write(this.nextButtonId);
            out.write("').disabled = true; \n");
         }

         //MODIFICATION disable transition buttons if save changes button is disabled (if mandatory variables are not filled)
         out.write("for(i=0; i<myButtons.length; i++){\n"+
                 "  $(myButtons[i]).disabled = true;\n"+
                 "}\n");
         //-MODIFICATION

         out.write("   }\n");
        
         out.write("   else\n   {\n      document.getElementById('");
         out.write(form.getClientId(context));
         out.write(NamingContainer.SEPARATOR_CHAR);
         out.write(getFinishButtonId());
         out.write("').disabled = false;");
        
         if (this.nextButtonId != null && this.nextButtonId.length() > 0)
         {
            out.write("\n      document.getElementById('");
            out.write(form.getClientId(context));
            out.write(NamingContainer.SEPARATOR_CHAR);
            out.write(this.nextButtonId);
            out.write("').disabled = false;");
         }

         //MODIFICATION disable transition buttons if save changes button is disabled (if mandatory variables are not filled)
         out.write("for(i=0; i<myButtons.length; i++){\n"+
                 "  $(myButtons[i]).disabled = false;\n"+
                 "}\n");
         //-MODIFICATION
        
         out.write("\n   }\n}\n\n");
      }
     
      // write out a function to initialise everything
      out.write("function initValidation()\n{\n");
     
      // register the validate function as the form onsubmit handler
      out.write("   document.getElementById('");
      out.write(form.getClientId(context));
      out.write("').onsubmit = validate;\n");
     
      // set the flag when the finish button is clicked
      out.write("   document.getElementById('");
      out.write(form.getClientId(context));
      out.write(NamingContainer.SEPARATOR_CHAR);
      out.write(getFinishButtonId());
      out.write("').onclick = function() { finishButtonPressed = true; }\n");
     
      // set the flag when the finish button is clicked
      if (this.nextButtonId != null && this.nextButtonId.length() > 0)
      {
         out.write("   document.getElementById('");
         out.write(form.getClientId(context));
         out.write(NamingContainer.SEPARATOR_CHAR);
         out.write(this.nextButtonId);
         out.write("').onclick = function() { nextButtonPressed = true; }\n");
      }
     
      // perform an initial check at page load time (if we have any real time validations)
      if (numberRealTimeValidations > 0)
      {
         out.write("   processButtonState();\n");
      }
     
      // close out the init function
      out.write("}\n\n");
     
      // setup init function to be called at page load time
      out.write("window.onload=initValidation;\n");
     
      // close out the script block
      out.write("</script>\n");
   }

Outcomes