Skip to content
April 7, 2013 / Pedro Gabriel

How to create your own Dynamic Form

ADF provides you the ability to create out-of-the-box dynamic forms. Nevertheless, one of these days I have been faced with the need to have a dynamic form with regular inputText components but in some of them I needed to search information in a modal in order to fill the component with data. Based on this requirement I had to create my own dynamic form. Today I am going to explain how I have done it.

I will make use of my previous post where I have showed how to create a declarative component with modal.

You can download this example here: CustomDynamicForm.

The first step you need to do is to create a java class that retains the configurations for each row of the form. For this purpose I called it “DynamicFormElement” and is defined as:

public class DynamicFormElement {
  private String attributeName;
  private Object attributeValue;
  private String attributeLabel;
  private String attributeType;
  private boolean attributeRequired;
  private String action;
  public DynamicFormElement() {     }

public void setAttributeName(String attributeName) {
    this.attributeName = attributeName;
  }
  public String getAttributeName() {
    return attributeName;
  }

  public void setAttributeValue(Object attributeValue) {
    this.attributeValue = attributeValue;
  }

public Object getAttributeValue() {
    return attributeValue;
  }

public void setAttributeType(String attributeType) {
    this.attributeType = attributeType;
  }

public String getAttributeType() {
    return attributeType;
  }

public void setAttributeRequired(boolean attributeRequired) {
    this.attributeRequired = attributeRequired;
  }

public boolean isAttributeRequired() {
    return attributeRequired;
  }

public void setAction(String action) {
    this.action = action;
  }

public String getAction() {
    return action;
  }

public void setAttributeLabel(String attributeLabel) {
    this.attributeLabel = attributeLabel;
  }

public String getAttributeLabel() {
    return attributeLabel;
  }
}

Then create a task flow like this:

CustomDynamicFormFlow

The initial method call gets the dynamic form elements with their properties. In this case I have created some dummy values.

public void getDynamicFormElements() {
customDynamicFormElements = new ArrayList();

DynamicFormElement element1 = new DynamicFormElement();

element1.setAttributeLabel("Integer");
element1.setAttributeName("attrInteger");
element1.setAttributeValue(123456789);
element1.setAttributeType("INTEGER");
element1.setAttributeRequired(true);
customDynamicFormElements.add(element1);

DynamicFormElement element2 = new DynamicFormElement();
element2.setAttributeLabel("Date");
element2.setAttributeName("attrDate");
element2.setAttributeValue(null);
element2.setAttributeType("DATE");
element2.setAttributeRequired(false);
customDynamicFormElements.add(element2);

DynamicFormElement element3 = new DynamicFormElement();
element3.setAttributeLabel("DeclComponent1");
element3.setAttributeName("attrDeclComponent1");
element3.setAttributeValue("SomeText");
element3.setAttributeType("DECLARATIVE_COMPONENT");
element3.setAttributeRequired(true);
element3.setAction("declComponent");
customDynamicFormElements.add(element3);

DynamicFormElement element4 = new DynamicFormElement();
element4.setAttributeLabel("DeclComponent2");
element4.setAttributeName("attrDeclComponent2");
element4.setAttributeValue("SomeText2");
element4.setAttributeType("DECLARATIVE_COMPONENT");
element4.setAttributeRequired(true);
element4.setAction("declComponent");
customDynamicFormElements.add(element4);
}

Then in the fragment we will iterate over the elements of the form previously populated. CustomDynamicFormFragment

In the “Search Type Component”, the declarative component, you need to set the methods “clearActionListener”, “searchActionListener”, “searchReturnListener”, and “SearchAction”.

public void clearAL(ActionEvent actionEvent) {
    setActionsValues(actionEvent, CLEAR_ACTION);
  }

  public void searchAL(ActionEvent actionEvent) {
    setActionsValues(actionEvent, SEARCH_ACTION_LISTENER);
  }

public void searchRL(ReturnEvent returnEvent) {
    setActionsValues(returnEvent, SEARCH_RETURN_LISTENER);
  }

public String searchAction() {
    return actionModal;
  }

private void setActionsValues(FacesEvent event, String actionType) {
  try
    {
      UIComponent component = event.getComponent();
      UIComponent parentComponent = component.getParent().getParent();
      CustomComponent customComp = (CustomComponent)parentComponent;
      Object attributeName = customComp.getKeyName();

      if (!DCUtils.isNullOrEmpty(attributeName)) {
        for (int i = 0; i < customDynamicFormElements.size() ; i++) {
          DynamicFormElement formElement = customDynamicFormElements.get(i);
          if (formElement.getAttributeName().equalsIgnoreCase(attributeName.toString())) {
            if (actionType.equalsIgnoreCase(CLEAR_ACTION))
            {
              formElement.setAttributeValue(null);
              customComp.clearValue();
              break;
            }
            else if (actionType.equalsIgnoreCase(SEARCH_ACTION_LISTENER))
            {
              actionModal = formElement.getAction();
              break;
            }
            else if (actionType.equalsIgnoreCase(SEARCH_RETURN_LISTENER))
            {
              formElement.setAttributeValue(modalReturnValue);
              customComp.setValue(modalReturnValue);
              break;
            }
          }
        }
      }
  }
  catch(Exception ex) {
    ex.printStackTrace();
  }
}

The final result looks like this:

CustomDynamicFormResult

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: