activitibpmnflowable

How to read form fields by formKey from Activiti/Flowable API


Using Flowable 6.4.0 (also applies to Activiti 6.0.0) I am rendering task form in my application by reading task form properties:

for (FormProperty formProperty : formService.getTaskFormData(taskId).getFormProperties()) {
//... render form field
}

I would like to replace this with Forms that can be designed in form designer and are set on task as formKey (also formReference, seems formReference is web app feature to select form to set formKey and deploy form to app). Problem is I do not see any API method to read form properties based on form key.

Reading answers in this question using formKey is correct way since task form properties are "deprecated", but is there API support for reading form fields for given formKey? Something like formService.getFormData(formKey, taskId).getFormProperties();?

Documentation (Flowable, Activiti) talks only about reading deployed form resource file my-custom-form.xml, does that mean I have to parse form file myself? I also checked Flowable Form Documentation and Sources but I do not see how to read form fields.


Solution

  • If you want to use forms designed with the Flowable Form Designer, then there are additional API methods to fetch a start form and task form in Flowable. The getTaskFormModel(String taskId) method in TaskService gives you for example the form definition associated with the Task with that id:

    https://github.com/flowable/flowable-engine/blob/master/modules/flowable-engine/src/main/java/org/flowable/engine/TaskService.java#L323

    If you look in the implementation of this method you can see that it uses the formKey attribute of the userTask element to lookup the form definition based on key.

    To get a start form of a process definition you can use the following code to lookup the formKey attribute of the startEvent element and get the form definition by key from the FormEngine:

    https://github.com/flowable/flowable-engine/blob/master/modules/flowable-rest/src/main/java/org/flowable/rest/service/api/repository/ProcessDefinitionResource.java#L145

    When you retrieved the FormInfo class from the getTaskFromModel method you can use the getFormModel method to retrieve the FormModel interface to get an instance of the Form model. Because the FormModel is made pluggable to make it easier to define your own Form model structure, you need to cast the FormModel interface to SimpleFormModel. From there you can use the getFormFields method to get to the form fields of the form definition.