Form Builder
Learn how to use the Form.io JavaScript form builder

Introduction

The Form.io Form Builder is an extension on the Form Renderer that allows for dynamic drag-and-drop building of the forms that will be rendered within the Form Renderer. The purpose of the form builder is to serve as a front-end user interface that is able to visually create the JSON schemas that are needed to be rendered into the form. For this reason, it can be thought of as a glorified JSON schema builder, where each field dragged onto the form creates the JSON schema of that component as it is configured. The following diagram illustrates this relationship between the form builder interface and the JSON schema that is being created.
Form.io Form Builder creating a JSON schema
For a great interactive visual on how the Form.io Form Builder works, we highly recommend visiting the Form Builder Demo @ https://formio.github.io/formio.js/app/builder to visually see how building a form and configuring the components can modify the JSON of the form in real time.

Embedding the Form Builder

One great benefit of the Form.io form builder is it can also be embedded into your application just like the form renderer. It is very similar to the form renderer, which just a few minor changes. First we will need to open up a new JSFiddle and input the following configurations.
Add the following Resources.
  • https://cdn.form.io/formiojs/formio.full.min.js
  • https://cdn.form.io/formiojs/formio.full.min.css
  • https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css
  • https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css
Add the following HTML
1
<div id="builder"></div>
Copied!
Add the following JavaScript.
1
Formio.builder(document.getElementById('builder'));
Copied!
Once you do this, you will then see your Form Builder show up in the result panel.
Illustrates how the form builder can be embedded in an application
This is a fully functional form building interface, which you can see by checking out the following JSFiddle.
Let's now explore all the options you can use to customize the form builder interface.

Form Builder SDK

In order to take advantage of the form builder, you need to understand all of the options available to you when working with the form builder interface. In all cases, the Form Builder is instantiated using the Formio.Builder method, which is defined as follows.

Formio.builder(element, [form], [options])

This method creates a new Form Builder interface within your application, and the following parameters can be applied.
Parameter
Description
element
The DOM element you would like to attach the form builder against.
form (optional)
Either the "src" or the form JSON you would like to use as the default form that shows up in the builder interface.
options (optional)
As an example, the following will create a new form builder, and then auto populate it with the form defined.
1
Formio.builder(document.getElementById('builder'), {
2
components: [
3
{
4
type: 'textfield',
5
key: 'firstName',
6
label: 'First Name'
7
},
8
{
9
type: 'textfield',
10
key: 'lastName',
11
label: 'Last Name'
12
}
13
]
14
});
Copied!
Which produces the following builder interface.
Optionally, you can provide the "src" to the form as follows.
1
Formio.builder(document.getElementById('builder'), 'https://examples.form.io/example');
Copied!
Which produces the following builder interface.
The Formio.Builder method returns a promise that will resolve with the Form Builder instance, which can be used as follows.
1
Formio.builder(document.getElementById('builder'), {
2
components: [
3
{
4
type: 'textfield',
5
key: 'firstName',
6
label: 'First Name'
7
},
8
{
9
type: 'textfield',
10
key: 'lastName',
11
label: 'Last Name'
12
}
13
]
14
}).then((builder) => {
15
16
// Here is the form builder instance.
17
console.log(builder);
18
});
Copied!
In a later section we will go through some of the methods that can be used on the builder instance that will further enable customizations of the form builder interface.

Form Builder Options

The options of the form builder are passed as a JavaScript object to the Formio.Builder method like the following illustrates.
1
Formio.builder(document.getElementById('formio'), 'https://examples.form.io/example', {
2
disabled: ['email']
3
});
Copied!
The following are a list of options that can be passed to the Form Builder instantiation method.
Property
Description
disabled
An array of "keys" of components that should be disabled within the form builder. Example: ['firstName', 'lastName']
noNewEdit
Boolean (true) that makes it so that there is no modal that is shown when a component is dragged onto the form.
noDefaultSubmitButton
Boolean (true) that will not show a submit button by default in the builder.
alwaysConfirmComponentRemoval
Boolean (true) that will make every component removal show a confirmation message if you are sure you wish to remove the component (like layout components do).
formConfig
Form configurations to apply forms being created. These configurations are added to the "config" property of the form object.
resourceTag
The tag to use to query for the "Existing Resource Fields" section of the builder.
editForm
Options to apply to the Edit Form which is the form that shows inside the modal when you edit a component.
language
The language of the builder

Builder Sidebar Configuration

These are the properties that can be set on the "builder" configuration option that is passed to the builder. This option is passed to the form builder like the following illustrates.
1
Formio.builder(document.getElementById('formio'), {}, {
2
builder: {
3
...
4
}
5
});
Copied!
The properties in this configuration allow for the manipulation of the Builder sidebar as well as control the default schemas and behaviors of the fields that are added to Form Builder. By default, this property has the following configuration.
1
{
2
resource: true, // Boolean to turn on or off "Existing Resource Fields" section
3
basic: {
4
title: 'Basic',
5
weight: 0,
6
default: true,
7
},
8
advanced: {
9
title: 'Advanced',
10
weight: 10
11
},
12
layout: {
13
title: 'Layout',
14
weight: 20
15
},
16
data: {
17
title: 'Data',
18
weight: 30
19
},
20
premium: {
21
title: 'Premium',
22
weight: 40
23
}
24
}
Copied!
Each of these sections can be turned off by simply adding a false for the value of that component.
For example, if we wish to turn off the Advanced, Premium, and Existing Resource Fields sections of the form builder sidebar, we could pass along the following configurations.
1
Formio.builder(document.getElementById('builder'), {}, {
2
builder: {
3
resource: false,
4
advanced: false,
5
premium: false
6
}
7
});
Copied!
This configuration can also be used to add Pre-defined components to the builder.

Adding Pre-Defined Components

A pre-defined component is a component that you provide the JSON configuration up front and then allow an end-user to drag-and-drop that component onto a form. A good example of this would be if you wish to have defined fields, such as "First Name" "Last Name" and "Email", instead of the field types, such as "Text Field", that are provided by default. Pre-defined fields can be added to the form builder using the builder as the following illustrates.
1
Formio.builder(document.getElementById('builder'), {}, {
2
builder: {
3
custom: {
4
title: 'Pre-Defined Fields',
5
weight: 10,
6
components: {
7
firstName: {
8
title: 'First Name',
9
key: 'firstName',
10
icon: 'terminal',
11
schema: {
12
label: 'First Name',
13
type: 'textfield',
14
key: 'firstName',
15
input: true
16
}
17
},
18
lastName: {
19
title: 'Last Name',
20
key: 'lastName',
21
icon: 'terminal',
22
schema: {
23
label: 'Last Name',
24
type: 'textfield',
25
key: 'lastName',
26
input: true
27
}
28
},
29
email: {
30
title: 'Email',
31
key: 'email',
32
icon: 'at',
33
schema: {
34
label: 'Email',
35
type: 'email',
36
key: 'email',
37
input: true
38
}
39
},
40
phoneNumber: {
41
title: 'Mobile Phone',
42
key: 'mobilePhone',
43
icon: 'phone-square',
44
schema: {
45
label: 'Mobile Phone',
46
type: 'phoneNumber',
47
key: 'mobilePhone',
48
input: true
49
}
50
}
51
}
52
}
53
}
54
});
Copied!
The default schema for each of these fields is defined in the schema property of that component, which means that as the user drags-and-drops the field on to the form builder, this is the schema that will be used to default that field on the form. The following configuration produces the following form builder.

Events

The following form builder events are emitted from the form builder as the user is building forms.
Event
Description
Arguments
addComponent
Triggered every time a component is added to the form via the builder drop operation.
  • info: The component information that was added to the form.
  • parent: The parent component information.
  • path: The component path for this dropped component
  • index: The index within the parent for this component.
  • isNew: Boolean to indicate if the component is "new" to the form. "false" means it was dragged and dropped from elsewhere on the form.
removeComponent
Triggered when a component is removed by the form builder.
  • component: The component being removed.
  • schema: The component minified json schema
  • path: The component path for this dropped component
  • index: the index within the parent for this component.
updateComponent
Triggered when a component has been updated within the form builder
  • component: The component being updated.
builderFormValidityChange
Triggered when the builder highlights some fields as invalid
  • hasInvalidComps: Boolean if there are invalid components.
Last modified 4mo ago