0

HTMLFormEntry is a very effective way of saving forms. You create the XML and the module handles transforming the input into database records upon pressing submit.

How can we render a form, e.g. at path/to/form.xml?

Do we have to render it in its own page or can we load it into a page?

Ulad Kasach
  • 11,558
  • 11
  • 61
  • 87

1 Answers1

0

1. Create a new form object from your xml

Option 1: Utilizing public void HtmlFormEntry.HtmlForm.setXmlData(String xmlData)

import org.openmrs.module.htmlformentry.HtmlForm;

...

HtmlForm form = new HtmlForm();
form.setXmlData(xml);

Option 2: if your XML is not loaded yet, utilizing public static HtmlForm HtmlFormEntryUI.HtmlFormUtil.getHtmlFormFromUiResource(<arguments>), reference

  • Note: relativeWebResourcePath is the relative path from the directory /omod/src/main/webapp/resources of your module.
  • Arguments are ResourceFactory resourceFactory, FormService formService, HtmlFormEntryService htmlFormEntryService, String providerAndPath
import org.openmrs.api.FormService;
import org.openmrs.module.htmlformentry.HtmlFormEntryService;
import org.openmrs.module.htmlformentry.HtmlForm;
import org.openmrs.module.htmlformentryui.HtmlFormUtil;

...

String htmlFormPath = thisModuleName+":"+relativeWebResourcePath;
ResourceFactory resourceFactory = ResourceFactory.getInstance();
FormService formService = Context.getFormService();
HtmlFormEntryService htmlFormEntryService = Context.getService(HtmlFormEntryService.class);
HtmlForm form = HtmlFormUtil.getHtmlFormFromUiResource(resourceFactory, formService, htmlFormEntryService, htmlFormPath);

2. Use FormEntrySession to generate HTML and handle form entry, submission, etc

import org.openmrs.module.htmlformentry.FormEntrySession;

...

FormEntrySession fes = new FormEntrySession(patient, form, request.session)
String html = fes.getHtmlToDisplay()

3. Use the html generated

The html that the FormEntrySession object generated should contain all the logic required to submit the form.

Ulad Kasach
  • 11,558
  • 11
  • 61
  • 87