Skip to content

Adding Form Rules

Benjamin Mwalimu edited this page Jul 9, 2020 · 1 revision

Adding Form Rules

The native form library uses Easy Rules - Rules Engine library for handling form skip logic (logic for determining which widget should be shown/displayed on the form depending on the given condition). It is also used to perform form calculations & add constraints to fields.

Easy Rules provides support for defining rules with MVEL. You can use either JSON or YAML specification. More about the expression language support Easy Rules expression language support. The native form library uses YAML to define the rules.

Example of skip logic, constraints and calculation rules using YML specification.

*Relevence

---
name: step1_delivery_date
description: delivery_date
priority: 1
condition: "step1_anc_close_reason == 'live_birth' || step1_anc_close_reason == 'stillbirth'"
actions:
- "isRelevant = true"
---

*Calculations

---
name: step2_sfh_ga_hidden
description: SFH edd
priority: 1
condition: "true"
actions:
  - "calculation = step2_sfh_gest_age > 0 ? step2_sfh_gest_age + ' weeks' : 0"

*constraints

---
name: step3_c_sections
description: C -Sections number selector
priority: 1
condition: "true"
actions:
- "constraint = step3_parity + 1"

Skip logic

Suppose you want to hide fields if a given condition is met, all you have to do is add a rule with a name as the unique reference of the key to be affected. The format is as follows stepName_fieldkey e.g step3_c_sections and specify the condition. For the action add "field_name_visibility = true". The native form library will search for the view with a tag matching that particular field_name and update its visibility to View.VISIBLE if the condition evaluates to true.

Sample code of how to add skip logic to a field

  "relevance": {
    "rules-engine": {
      "ex-rules": {
        "rules-file": "profile_relevance_rules.yml"
      }
    }
  }

Calculation

Suppose you want to assign a certain field value or get a calculation and the result assigned as the field value, all you have to do is add a rule with a name as the unique reference of the key to be affected. The format is as follows stepName_fieldkey e.g step2_sfh_ga_hidden and specify the condition. For the action add the calculation to be performed e.g step2_sfh_gest_age > 0 ? step2_sfh_gest_age + ' weeks' : 0" or "calculation = step3_live_births + step3_stillbirths". The native form library will search for the view with a tag matching that particular field_name and update its value to the value generated after the calculation is made if the condition evaluates to true.

Sample code of how to add calculation to a field

  "calculation": {
    "rules-engine": {
      "ex-rules": {
        "rules-file": "profile_calculation_rules.yml"
      }
    }
  }

Constraints

Currently works with number_selector fields. In case you want to set a maximum number for the number selector field, all you have to do is add a rule with a name as the unique reference of the key to be affected. The format is as follows stepName_fieldkey e.g step3_c_sections and specify the condition. For the action add the calculation to be performed e.g "constraint = step3_parity + 1". The native form library will search for the view with a tag matching that particular field_name and update its max_value to the value generated after the calculation is made if the condition evaluates to true.

Sample code of how to add constraints to a field

  "constraints": {
    "rules-engine": {
      "ex-rules": {
        "rules-file": "profile_constraint_rules.yml"
      }
    }
  }