Client-Side Custom Forms
Overview
Custom forms can only run be configured on the client side. Server-side configuration is not yet available. This approach allows for greater flexibility and control over the form’s behavior and appearance. By leveraging client-side configuration, developers can create dynamic and interactive forms enhancing the user experience.
How to Set Up Client-Side Custom Forms
- Embed the Chatbot using the new Script:
To ensure that you can register client-side custom forms, you need to embed the Chatbase script correctly.
Follow these steps to get the embed code:
- Visit the Chatbase Dashboard.
- Navigate to Connect > Embed > Embed code with identity.
- Copy the embed code and add it to your application.
Note: If you don’t need to verify the identity of logged-in users, you can skip the first script in the embed code and only use the chatbot embed script.
Here’s an example of the embed code you’ll find on the dashboard:
- Create the Client-Side Custom Form:
To create a client-side custom form:
- Visit the Chatbase Dashboard
- Navigate to Actions > Create action > Custom form.
- Fill in the required fields, ensuring the form type is set to client-side.
-
Use the SDK
registerFormSchema
method:Load the Chatbase script and call the
registerFormSchema
method with all the form fields. The form names must match those written in the custom form page.
Method Parameters
Each custom form method receives two parameters:
args
: Contains all the arguments defined in your custom form configurationuser
: Contains the authenticated user information if identity verification is properly configured
Method Return Schema
The registerFormSchema
function returns a schema that defines the structure and validation of the custom form. Below is a table detailing each field in the return schema:
Field Name | Explanation | Requirements |
---|---|---|
fields | An array of form field definitions, each specifying the field’s properties. | Must be an array of objects conforming to Field Schema defined below. |
submitButtonText | Optional text for the submit button. Default text is “Submit”. | Must be a string if provided. |
successMessage | Optional message displayed upon successful form submission. Default message is “Form submitted successfully”. | Must be a string if provided. |
errorMessage | Optional message displayed if form submission fails. Default message is “Error submitting form”. | Must be a string if provided. |
Field Schema
Each field in the fields
array must adhere to the following structure:
Field Property | Explanation | Requirements |
---|---|---|
name | The unique identifier for the form field. | Must be a string. |
type | The type of the form field. | Must be one of the available Field Types defined below. |
label | The display label for the form field. | Must be a string. |
defaultValue | The default value for the form field. | Can be any type, optional. |
validation | An object defining validation rules for the field. | Must be a strict object with optional properties for each validation rule following the Validation Rules defined below. |
options | An array of options available for selection. | Required for select and multiselect type fields. Must be an array of objects with label and value properties. |
Field Types
Field Type | Explanation | Requirements |
---|---|---|
text | A single-line text input. | Validation rules can include required , minLength , maxLength , pattern . |
textarea | A multi-line text input. | Validation rules can include required , minLength , maxLength . |
email | An input for email addresses. | Validation rules can include required , pattern . |
phone | An input for phone numbers. | Must follow the format /^\+[1-9]\d{1,14}$/ (a valid phone number starting with + and country code (e.g., +1234567890)). Validation rules can include required , pattern . |
number | An input for numeric values. | Validation rules can include required , min , max . |
select | A dropdown selection input. | Must include options array. Validation rules can include required . |
multiselect | An input allowing multiple selections. | Must include options array. Validation rules can include required . |
image | An input for image file uploads. | Accepts only 'image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp' and has a maximum size of 2MB. Validation rules can include required . |
Validation Rules
The validation
object can include different validation rules. Each rule is an object with a value
and message
property. The message
property indicates the error message to be displayed if the validation rule is violated. There is also a default error message field that will be displayed if the validation rule is violated and no custom message is provided. The following rules are available:
Validation Rule | Explanation | Requirements |
---|---|---|
required | Specifies if the field is mandatory. | Must be an object with value (boolean) and message (string). |
pattern | A regex pattern the field value must match. | Must be an object with value (string) and message (string). |
min | Minimum numeric value allowed for the field. | Must be an object with value (number) and message (string). |
max | Maximum numeric value allowed for the field. | Must be an object with value (number) and message (string). |
minLength | Minimum length of the field value. | Must be an object with value (number) and message (string). |
maxLength | Maximum length of the field value. | Must be an object with value (number) and message (string). |
defaultErrorMessage | Default error message if validation fails. | Must be a string, optional. |
Webhooks
You can configure webhooks for form submission events in the custom form action tab or in the Settings > Webhooks section. Learn more about webhooks.
Important Notes
Multiple Registrations: Calling registerFormSchema
multiple times will override previous forms. Make sure to provide all desired forms in a single registerFormSchema
call.
Environment Limitations: Client-side custom forms will not work in the Chatbase Playground, Action Preview, or Compare features, as these environments don’t support client-side code execution.