Hopefully, you read my last post about creating Field Groups. This post shows how to create a standard field defnitions that can reused over and over in form definitions.

My field definitions look like this in my XML:

  <form name="FieldDefinitions">
    <field property="PROJECT_NAME" depends="required,alphanumeric,maxLength">
       <arg0 key="attributes.project_name.displayname"/>
       <var><var-name>maxLength</var-name><var-value>100</var-value></var>
       <var><var-name>allowSpaces</var-name><var-value>T</var-value></var>
    </field>
    <field property="PROJECT_DESCRIPTION" depends="alphanumeric">
       <arg0 key="attributes.project_description.displayname"/>
       <var><var-name>maxLength</var-name><var-value>1000</var-value></var>
       <var><var-name>allowSpaces</var-name><var-value>T</var-value></var>
    </field>
  </form>

  <form name="FormOne" >
    <field property="PROJECT_NAME" depends="required" fieldRef="PROJECT_NAME" />
  </form>

The result of my modification is that the depends attribute of the Field Definition is appended to the depends information of the PROJECT_NAME field. Additionally the arg0 element and var elements are copied to the PROJECT_NAME field.
The following description of how I implemented my change assumed that you already know how to compile the Validator project. Also note that errors are not handled because I use a JUnit test that is run during an automated build and regression test to ensure that the XML files are correct. So the error checking code should never be implemented.

  1. Add public FastHashMap getHForms() { return hForms; } to the FormSet class.
  2. Add protected String fieldRef; with the appropriate getter and setter methods to the Field class.
  3. After the XML files are processed, run the following code:
        // The Formsets are stored under Locale keys. Store
        // the form object into a hashmap for later processing.
        Iterator iFormSets = ((Vector)resources.getValidatorFormSet().get("en_US")).iterator();
        while (iFormSets.hasNext()) {
          FormSet fs = ((FormSet)iFormSets.next());
          Iterator iForms = fs.getHForms().keySet().iterator();
          while (iForms.hasNext()) {
            String formName = (String)iForms.next();
            formObjects.put(formName, fs.getHForms().get(formName));
          }
        }
    
        Form fieldDefinitions = resources.get(Locale.getDefault(), "FieldDefinitions");
        Map fieldDefinitionMap = fieldDefinitions.getFieldMap();
    
        // Now iterate over the forms looking for formRefList
        // attributes.
        Iterator iForms = formObjects.keySet().iterator();
        while (iForms.hasNext()) {
          String formName = (String)iForms.next();
          Form form = (Form)formObjects.get(formName);
          // see if any fields refer to field definitions.
          List lFields = form.getFields();
          if (lFields == null) {
            System.out.println("No fields in form [" + formName + "].");
          } else {
            Iterator iFields = lFields.iterator();
            while (iFields.hasNext()) {
              Field field = (Field)iFields.next();
              String fieldRef = field.getFieldRef();
              if (fieldRef != null && fieldRef.length() > 0) {
                Field referencedField = (Field)fieldDefinitionMap.get(fieldRef);
                String referencedDepends = referencedField.getDepends();
                if (referencedDepends.length() > 0) {
                  String depends = field.getDepends();
                  if (depends != null && depends.length() > 0) {
                    field.setDepends(depends + "," + referencedDepends);
                  } else {
                    field.setDepends(referencedDepends);
                  }
                }
                field.addArg0(referencedField.getArg0());
                Map referencedFieldVars = referencedField.getVars();
                Iterator iReferencedFieldVars = referencedFieldVars.keySet().iterator();
                while (iReferencedFieldVars.hasNext()) {
                  String varName = (String)iReferencedFieldVars.next();
                  field.addVar(referencedField.getVar(varName));
                }
                //System.out.println(field);
              }
            }
          }
    
        }
    
    </ol>