Skip to content

Validation Rule

The validation rule in Emakin ensures data accuracy and integrity by validating widget input based on predefined conditions. When a value fails validation, the system provides appropriate feedback to the user, helping maintain consistent and accurate data entry.

Validation Group Name

Validation rules are executed based on group identifiers, which link validation conditions to specific user actions.

A group name identifier specifies the context in which a validation rule should run. Example: Assigning save as a group name triggers the validation rule when the user clicks the Save button.

Default Execution

  • If no group name is defined, the validation rule executes for all groups, regardless of the triggering action.

Multiple Group Names

Multiple group names can be specified using a semicolon (;) as a separator. Example: Defining save;submit runs the validation rule when either the Save or Submit button is clicked.

Best Practices for Defining Validation Rules

  • Use meaningful group names that reflect the corresponding user action (e.g., submit, approve, finalize).
  • Ensure rules cover all required actions to prevent unintended data submission.
  • Avoid overlapping group names unless the same validation logic applies across multiple actions.
  • Avoid updating the data model within validation rules to maintain separation of concerns.

By implementing validation rules with clear group identifiers, Emakin forms offer robust data validation, ensuring that only accurate and properly formatted data is submitted.

Error Message

When validation rule condition are met, Emakin displays an error message to the user. This message can be customized to provide specific feedback on the validation failure.

Rule Condition

Validation rule conditions are defined using JavaScript functions that return a boolean value. These functions are triggered when changes occur in the data model, updating the widget accordingly.

Condition scripts support the async/await pattern for asynchronous operations. This allows validation logic to include external API calls or long-running tasks.

Available Scripting Variables

  • $Xml Represents the XML data model element bound to the widget.
  • $ActiveUser Contains current user identity details.
  • $Disabled Indicates the form's disabled state.
  • $Process The ID of the currently running process.
  • $StaticUrl The URL for accessing static files.

Structure of $ActiveUser JSON Object:

1
2
3
4
5
6
7
{
    "name": "<user_display_name>",
    "type": "User",
    "domain": "<domain_id>",
    "id": "<user_id>",
    "email": "<user_email>"
}

Warning

The $Disabled variable indicates the form's disabled state, not the individual widget's state.

Example

The following example demonstrates a validation rule that ensures a numeric field contains at least 10 items:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<NumericBox XPath="amount">
    <Rules>
        <ValidationRule Group="save;submit">
            <Condition>
                <Content><![CDATA[return $Xml.EvaluateNumber() < 10;]]></Content>
                <Definitions/>
            </Condition>
            <Label>
                <Content>Please enter at least 10 items</Content>
            </Label>
        </ValidationRule>
    </Rules>
</NumericBox>

Regular Expression Validation

To use regular expressions for validation, you may use the RegExp object in JavaScript. The following example demonstrates a validation rule that ensures a text box contains a valid email address:

1
return (!$Xml.Evaluate('.').match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi))