Search code examples
jsfwebmethodswebmethods-caf

Task with Save button throws validation errors


I have a webMethods CAF task that has a big form with a save button and a submit button. Many elements on the form have validation. The user needs to be able to hit Save and have the form submitted to the back end model so it can be saved as task data, without firing validation. Hitting Submit should fire validation.

How can I configure the page to do this. It's such a normal requirement, and I'm stuck!


Solution

  • It's not much fun.

    1. Give your Save button a nice ID. Say, saveButton
    2. Create a getter in your Java code that returns a boolean. Inside it, return true if the button's ID is one of the submitted fields, otherwise false:

      private boolean validationRequired() {
          return mapValueEndsWith((Map<String, String>)getRequestParam(),
              new String[] {
                  "saveButton",           // Your save button
                  "anotherButton",        // Perhaps another button also shouldn't validate
                  "myForm:aThirdButton"   // perhaps you want to be specific to a form
              });
      }
      
    3. In every field that should be required, except on Save, bind the Validation->required attribute to your validationRequired getter.

    That's it! Very tedious with a lot of fields on the screen, but it works.

    P.s. what's mapValueEndswith? Just a utility; removed whitespace for compactness' sake:

    private boolean mapValueEndsWith(Map<String, String> haystack, String[] needles) {
        for(String needle : needles) if(mapValueEndsWith(haystack, needle)) return true;
        return false;
    }
    
    private boolean mapValueEndsWith(Map<String, String> haystack, String needle) {
        for(String value : haystack.values()) if(value.endsWith(needle)) return true;
        return false;
    }