Dynamic Field Addition in Enterprise Forms HST Component at Runtime
On this page
Introduction
Sometimes, you may just want to add some extra field(s)/fieldgroups/pages when rendering/storing the form based on user/session state information or whatever, without having to define them explicitly in the form document.
It is very easy to override some methods of the Enterprise Forms HST Component to add extra fields at runtime.
Examples in the Demo Project
Dynamic Field Creation
In the Enterprise Forms Demo projects, there is an example to show this scenario.
Here's how to test it out the example scenario:
- Build and run the Enterprise Forms Demo (access to Bloomreach Experience Manager Maven repository required).
- Visit http://localhost:8080/site/dynamicfields
- Enter all the fields and save.
- Now, see the data stored in the repository in the Form data application in CMS.
- Now, visit http://localhost:8080/site/dynamicfields?region=us
Note the region parameter this time. - See if there's an extra field named 'State' inserted before the postal code field in the form.
- Enter all the fields and save.
- Now, see the data stored in the repository in the Form data application in CMS again.
- See if you have the state field value for the second posting.
As you can check in the associated form document, there's no 'State' field definition in the form itself.
However, you can add an extra field very easily in your HST Component.
The following example code ( site/components/src/main/java/com/onehippo/cms7/eforms/demo/components/DynamicFieldsEformComponent.java in the demo) shows how to achieve this kind of scenario easily by overriding the #parse(final FormBean bean, ...) method:
Whenever the form is rendered (in #doBeforeRender()) or the form is submitted (in #doAction()), the #parse(final FormBean bean, ...) method is invoked to translate the form bean to runtime form/field instances.
So, the example component above is simply adding an extra field in an overridden method for #parse(final FormBean bean, ...) method. In this example, it simply checks the existence of a specific region request parameter to determine to show the extra field (see #isStateFieldNecessary() method), but you can replace it by something more meaningful/sophisticated for your business needs.
Also, note that you can create a field instance like #createStateTextField() does above, but also you can extend the existing field implementation to fulfil more complex scenarios.
Dynamic Field Group and Page creations
You can also add dynamic field groups and pages at runtime. They are demonstrated in the example form http://localhost:8080/site/dynamicregistration.
- Visit http://localhost:8080/site/dynamicregistration
- Enter all the fields and save.
- Now, see the data stored in the repository in the Form data application in CMS.
- Now, visit localhost:8080/site/dynamicregistration?people=4
Note the number of registering peole this time. - See if there are multiple pages, each for a registering person.
- Enter all the fields and save.
- Now, see the data stored in the repository in the Form data application in CMS again.
- See if you have registering people information for the second posting
The following example code ( site/components/src/main/java/com/onehippo/cms7/eforms/demo/components/DynamicRegistrationEformComponent .java in the demo) shows how to achieve this kind of scenario easily by overriding the #parse( FormBean, FormContext, HstRequest ) method: