Examples menu

Examples / Chat widget

Validation in the chat widget

The chat checks every answer against the form's own rules, and when one does not fit, it explains why in the form's words and asks again.

A chat fills in the same form, so the same rules apply. Every value the UAG takes from a message is checked against the form's validation: required fields, types, minimums and maximums, and patterns, using the form's own error messages. A value the form refuses is not kept; the chat says why and asks again. Nothing invalid can be submitted, because the save goes through the same checks.

This form wants a quantity from 1 to 10, an order code like ABC-1234, and a valid email. The first sample gives an order code without letters and a quantity over 10; the second gives the code in the wrong format and spells out the email. The AI may fix some answers on its own; whatever the form still refuses, the chat asks for again. Correct the answers in the chat.

Turn it on

json
{
  "title": "Example: Validation in the chat widget",
  "name": "exampleChatValidation",
  "path": "example-chat-validation",
  "type": "form",
  "display": "form",
  "tags": [
    "uag"
  ],
  "properties": {
    "description": "An order change: the order code, a new quantity, and an email for the confirmation."
  },
  "components": [
    {
      "type": "textfield",
      "key": "orderCode",
      "label": "Order code",
      "input": true,
      "validate": {
        "required": true,
        "pattern": "^[A-Z]{3}-\\d{4}$",
        "customMessage": "An order code is three capital letters, a dash and four digits, such as ABC-1234."
      }
    },
    {
      "type": "number",
      "key": "quantity",
      "label": "New quantity",
      "input": true,
      "validate": {
        "required": true,
        "min": 1,
        "max": 10
      }
    },
    {
      "type": "email",
      "key": "email",
      "label": "Email for the confirmation",
      "input": true,
      "validate": {
        "required": true
      }
    },
    {
      "type": "button",
      "key": "submit",
      "label": "Submit",
      "action": "submit",
      "input": true
    }
  ]
}

Try it

Samples:
Example: Validation in the chat widget

Read about it in the developer docs