There are different types of forms which are primarily used in Tasklist. To implement a task form in your application, you have to connect the form resource with the BPMN 2.0 element in your process diagram. Suitable BPMN 2.0 elements for calling tasks forms are the StartEvent and the UserTask.

Out of the box, Camunda Tasklist supports four different kinds of task forms:

This section applies to forms in Camunda Tasklist

When embedding the process engine into a custom application, you can integrate the process engine with any form technology such as [JavaServer Faces][jsf-task-forms], Java Swing, JavaFX, REST-based JavaScript web applications and many more.

Embedded Task Forms

Embedded task forms are HTML and JavaScript forms which can be displayed directly within Tasklist. We provide more information about the creation of embedded forms in our Embedded Task Forms Reference.

To add an embedded form to your application, simply create an HTML file and refer to it from a UserTask or a StartEvent in your process model. For example, you can create a FORM_NAME.html file containing the relevant content for your form, e.g., a simple form with two input fields:

  1. <form role="form" name="form">
  2. <div class="form-group">
  3. <label for="customerId-field">Customer ID</label>
  4. <input required
  5. cam-variable-name="customerId"
  6. cam-variable-type="String"
  7. class="form-control" />
  8. </div>
  9. <div class="form-group">
  10. <label for="amount-field">Amount</label>
  11. <input cam-variable-name="amount"
  12. cam-variable-type="Double"
  13. class="form-control" />
  14. </div>
  15. </form>

The file containing the form can be referenced in two ways:

  • app:: Add the file to your development project in a folder src/main/webapp/forms. The HTML file will be packaged into your deployment artifact (typically a WAR archive). During runtime it will be loaded from there.
  • deployment:: The file is part of your deployment (e.g., by adding it to your process archive), which means that it is stored in the Camunda database. It can then be loaded from there. Note that this allows versioning of your form alongside the process model.
    To configure the form in your process, open the process with the Camunda Modeler and select the desired UserTask or StartEvent. Open the properties panel and enter embedded:app:forms/FORM_NAME.html (or embedded:deployment:forms/FORM_NAME.html) as Form Key. The relevant XML tag looks like this:
  1. <userTask id="theTask" camunda:formKey="embedded:app:forms/FORM_NAME.html"
  2. camunda:candidateUsers="John, Mary"
  3. name="my Task">

Generated Task Forms

The Camunda process engine supports generating HTML task forms based on Form Data Metadata provided in the BPMN 2.0 XML. Form Data Metadata is a set of BPMN 2.0 vendor extensions provided by Camunda, allowing you to define form fields directly in the BPMN 2.0 XML:

  1. <userTask id="usertask" name="Task">
  2. <extensionElements>
  3. <camunda:formData>
  4. <camunda:formField
  5. id="firstname" label="Firstname" type="string">
  6. <camunda:validation>
  7. <camunda:constraint name="maxlength" config="25" />
  8. <camunda:constraint name="required" />
  9. </camunda:validation>
  10. </camunda:formField>
  11. <camunda:formField
  12. id="lastname" label="Lastname" type="string">
  13. <camunda:validation>
  14. <camunda:constraint name="maxlength" config="25" />
  15. <camunda:constraint name="required" />
  16. </camunda:validation>
  17. </camunda:formField>
  18. <camunda:formField
  19. id="dateOfBirth" label="Date of Birth" type="date" />
  20. </camunda:formData>
  21. </extensionElements>
  22. </userTask>

Form metadata can be graphically edited using the Camunda Modeler.

This form would look like this in Tasklist:

User Task Forms - 图3

As you can see, the <camunda:formData … /> element is provided as a child element of the BPMN <extensionElements> element. Form metadata consists of multiple form fields which represent individual input fields where a user has to provide some value or selection.

A form data can have following attributes:

Attribute Explanation
businessKey Id of a form field that will be marked as cam-business-key

Form Fields

A form field can have the following attributes:

Attribute Explanation
id Unique id of the form field, corresponding to the name of the process variable to which the value of the form field is added when the form is submitted.
label The label to be displayed next to the form field.
type The data type of the form field. The following types are supported out of the box:
  • string

  • long

  • date

  • boolean

  • enum

|defaultValue|Value to be used as a default (pre-selection) for the field.

Form Field Validation

Validation can be used for specifying frontend and backend validation of form fields. Camunda BPM provides a set of built-in form field validators and an extension point for plugging in custom validators.

Validation can be configured for each form field in the BPMN 2.0 XML:

  1. <camunda:formField
  2. id="firstname" name="Firstname" type="string">
  3. <camunda:validation>
  4. <camunda:constraint name="maxlength" config="25" />
  5. <camunda:constraint name="required" />
  6. </camunda:validation>
  7. </camunda:formField>

As you can see, you can provide a list of validation constraints for each form field.

The following built-in validators are supported out of the box:

Validator Explanation
required

Applicable to all types. Validates that a value is provided for the form field. Rejects 'null' values and empty strings.


<camunda:constraint name="required" />

|minlength|
Applicable to string fields. Validates the minimum length of text content. Accepts 'null' values.


<camunda:constraint name="minlength" config="4" />

|maxlength|
Applicable to string fields. Validates the maximum length of text content. Accepts 'null' values.


<camunda:constraint name="maxlength" config="25" />

|min|
Applicable to numeric fields. Validates the minimum value of a number. Accepts 'null' values.


<camunda:constraint name="min" config="1000" />

|max|
Applicable to numeric fields. Validates the maximum value of a number. Accepts 'null' values.


<camunda:constraint name="max" config="10000" />

|readonly|
Applicable to all types. Makes sure no input is submitted for the given form field.


<camunda:constraint name="readonly" />

Camunda BPM supports custom validators. Custom validators are referenced using their fully qualified classname or an expression. Expressions can be used for resolving Spring or CDI @Named beans:

  1. <camunda:formField
  2. id="firstname" name="Firstname" type="string">
  3. <camunda:validation>
  4. <camunda:constraint name="validator" config="com.asdf.MyCustomValidator" />
  5. <camunda:constraint name="validator" config="${validatorBean}" />
  6. </camunda:validation>
  7. </camunda:formField>

A custom validator implements the org.camunda.bpm.engine.impl.form.validator.FormFieldValidator interface:

  1. public class CustomValidator implements FormFieldValidator {
  2. public boolean validate(Object submittedValue, FormFieldValidatorContext validatorContext) {
  3. // ... do some custom validation of the submittedValue
  4. // get access to the current execution
  5. DelegateExecution e = validatorContext.getExecution();
  6. // get access to all form fields submitted in the form submit
  7. Map<String,Object> completeSubmit = validatorContext.getSubmittedValues();
  8. }
  9. }

If the process definition is deployed as part of a ProcessApplication deployment, the validator instance is resolved using the process application classloader and / or the process application Spring Application Context / CDI Bean Manager, in case of an expression.

External Task Forms

If you want to call a task form that is not part of your application, you can add a reference to the desired form. The referenced task form will be configured in a way similar to the embedded task form. Open the properties view and enter FORM_NAME.html as form key. The relevant XML tag looks like this:

  1. <userTask id="theTask" camunda:formKey="app:FORM_NAME.html"
  2. camunda:candidateUsers="John, Mary"
  3. name="my Task">

Tasklist creates the URL by the pattern:

  1. "../.." + contextPath (of process application) + "/" + "app" + formKey (from BPMN 2.0 XML) + "processDefinitionKey=" + processDefinitionKey + "&callbackUrl=" + callbackUrl;

When you have completed the task, the call back URL will be called.

How To

How to add JSF Forms to your process application

Generic Task Forms

The generic form will be used whenever you have not added a dedicated form for a UserTask or a StartEvent.

User Task Forms - 图4

Hit the Add a variable button to add a variable that will be passed to the process instance upon task completion. State a variable name, select the type and enter the desired value. Enter as many variables as you need.After hitting the Complete button, the process instance contains the entered values. Generic task forms can be very helpful during the development stage, so you do not need to implement all task forms before you can run a workflow. For debugging and testing this concept has many benefits as well.

You can also retrieve already existing variables of the process instance by clicking the Load Variables button.

原文: https://docs.camunda.org/manual/7.9/user-guide/task-forms/