-
Notifications
You must be signed in to change notification settings - Fork 28
5.2. Adding Rules
Neat form 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.
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
Example of skip logic and calculation rules using YML
specification.
---
name: "child_visibility"
description: "children are not allowed to buy alcohol"
priority: 1
condition: "age <= 18"
actions:
- "child_visibility = true"
---
name: "decade_calculation"
description: "decade calculation"
priority: 1
condition: "true"
actions:
- "import java.math.BigInteger;"
- "decade_calculation = age * 10"
Suppose you want to hide fields if a given condition is met, all you have to do is add a rule with a name ending with a suffix _visibility
and specify the condition. For the action add "field_name_visibility = true"
. Neat form will take care of the rest. Internally neat form will search for the view with a tag matching that particular field_name and update its visibility
to View.VISIBLE
. By default any field that has a visibility rule is hidden and will only be shown when the condition specified on the rule evaluates to true.
Calculations in NeatForm are treated as normal variables.Their rules are handled the same way as skip logic's, the only difference is that they end with a suffix _calculation
. The results of the calculation will be returned when the form is finally submitted. Fields can also watch for changes in the calculations.
For the following field types: number_selector
, text_input_edit_text
, edit_text
and datetime_picker
, their values are stored using primitive data types which can be accessed directly. For example age > 21
NOTE: Date and time views values are saved as
timestamps
spinner
, checkbox
and radio_group
field types save their value in a map with the key (field_name) and value (label of the selected option)
NOTE: To access label of a spinner use
spinner_field_name.value
multi_choice_checkbox
field type stores a list of map of the selected options. For instance if you had 3 options and the user selected options 1 and 2. The result would be a list of map of option 1 (option_1_key -> option_1_label) and option 2 (option_2_key -> option_2_label)
NOTE: if a field does not contain a value then its result will be
null
Created by Elly Nerdstone with the help of some awesome contributors. License Apache 2.0