Umbraco Forms comes with some built-in fields however it is possible to exclude/remove them if necessary. There might some use cases where you have no use for file upload and don't want editors using them. Or perhaps you want to remove a field to replace it with one with enhanced functionality that you build yourself.
The following class shows how to exclude built-in field types using a custom composer. The Password
, Recaptcha2
and RichText
field types (or "answers") will no longer be available for selection when creating a form in the backoffice.
This builds on the "adding a type to the provider model" chapter
In this article, we will illustrate how to add a custom form field type using server-side and client-side components. We will use the example of rendering a "slider" field type that allows the user to select a number within a specific range of values.
Add a new class to the Visual Studio solution. Inherit from Umbraco.Forms.Core.FieldType
and complete as follows:
In the constructor or via overridden properties, we can specify details of the field type:
Id
- should be set to a unique GUID.
Alias
- an internal alias for the field, used for localized translation keys.
Name
- the name of the field presented in the backoffice.
Description
- the description of the field presented in the backoffice.
Icon
- the icon of the field presented in the backoffice form builder user interface.
DataType
- specifies the type of data stored by the field. Options are String
, LongString
, Integer
, DataTime
or Bit
(boolean).
SupportsMandatory
- indicates whether mandatory validation can be used with the field (defaults to true
).
MandatoryByDefault
- indicates whether the field will be mandatory by default when added to a form (defaults to false
).
SupportsRegex
- indicates whether pattern-based validation using regular expressions can be used with the field (defaults to false
).
SupportsPreValues
- indicates whether prevalues are supported by the field (defaults to false
).
RenderInputType
- indicates how the field should be rendered within the theme as defined with the RenderInputType
enum.
The default is Single
for a single input field.
Multiple
should be used for multiple input fields such as checkbox lists.
Custom
is used for fields without visible input fields.
FieldTypeViewName
- indicates the name of the partial view used to render the field on the website.
EditView
- indicates the name of a property editor UI that is used for editing the field in the backoffice. If nothing is provided, the built-in label will be used and the field won't be editable.
PreviewView
- indicates the name of a manifest registered client-side resource that is used for previewing the field in the backoffice. If nothing is provided, the name of the field type will be used as the preview.
You now need to register this new field as a dependency:
We will start building the view for the default theme of the Form at Views\Partials\Forms\Themes\default\FieldTypes\FieldType.Slider.cshtml
.
The file name for the partial view should match the value set on the FieldTypeViewName
property.
This will be rendered when the default theme is used.
If working with Umbraco 9 or earlier versions, you'll find the Views\Partials\Forms\Themes\default\
folder on disk and can create the files there.
For Umbraco 10 and above, we've moved to distributing the theme as part of a Razor Class Library so the folder won't exist. However, you can create it for your custom field type. If you would like to reference the partial views of the default theme, you can download them as mentioned in the Themes article.
When rendering a multi-page form, editors have the option to display a summary page where the entries can be viewed before submitting.
To support this, a read-only view of the field is necessary.
For most fields, nothing is required here, as the default read-only display defined in the built-in ReadOnly.cshtml
file suffices.
However, if you want to provide a custom read-only display for your field, you can do so by creating a second partial view. This should be named with a .ReadOnly
suffix. For this example, you would create FieldType.Slider.ReadOnly.cshtml
.
Field settings will be managed in the backoffice by editors who will create forms using the custom field type. These settings can be added to the C# class as properties with a Setting
attribute:
The property Name
names the setting in the backoffice with the Description
providing the help text. Both of these can be translated, as discussed in the backoffice components section below.
The View
property indicates a property editor UI used for editing the setting value. You can use a built-in property editor UI, one from a package, or a custom one registered with your solution. The default value if not provided is Umb.PropertyEditorUi.TextBox
, which will use the standard Umbraco text box property editor UI.
SupportsPlaceholders
is a flag indicating whether the setting can contain "magic string" placeholders and controls whether they are parsed on rendering.
HtmlEncodeReplacedPlaceholderValues
takes effect only if SupportsPlaceholders
is true
. It controls whether the replaced placeholder values should be HTML encoded (as is necessary for rendering within content from a rich text editor).
SupportsHtml
is a flag indicating whether the setting can contain HTML content. When set to true
it will be treated as HTML content when the value is read from the Forms delivery API.
IsMandatory
if set to true
will provide client-side validation in the backoffice to ensure the value is completed.
When creating a field or other provider type, you might choose to inherit from an existing class. This could be if one of the types provided with Umbraco Forms almost meets your needs but you want to make some changes.
All setting properties for the Forms provider types are marked as virtual
, so you can override them and change the setting values:
With Forms 14, aspects of the presentation and functionality of the custom field are handled by client-side components, registered via manifests:
The preview, displayed on the form definition editor.
The property editor UI used for editing the the submitted values via the backoffice.
The property editor UI used for editing settings.
A settings converter, that handles configuring the property editor and translating between the editor and persisted values.
Translations for setting labels and descriptions.
To display a name and description on a custom field, you need to register a JavaScript file as shown in the Localization article.
The alias of the preview to use is defined on the field type via the PreviewView
property.
A preview for our slider, representing the selected setting values could look as follows:
And it is registered via a manifest:
Umbraco Forms supports editing of the entries submitted by website visitors via the backoffice. The property editor interface to use for this is defined in the field type's EditView
property.
If not using a built-in property editor, you can create your own. The following example shows how the numerical entries could be edited using an input control.
Again, it's registered via a manifest.
Field type settings also use a property editor UI for editing the values in the backoffice. The one to use is defined via the View
property on the Setting
attribute.
In our example we use a custom one, allowing the value for the background color to the field to be selected via an input control.
And register it via a manifest:
You may want to consider registering a settings value converter. This is another client-side component that is registered in a manifest. It converts between the setting value required for the editor and the value persisted with the form definition. A converter defines three methods:
getSettingValueForEditor
- converts the persisted string value into one suitable for the editor
getSettingValueForPersistence
- converts the editor value into the string needed for persistence
getSettingPropertyConfig
- creates the configuration needed for the property editor
The following code shows the structure for these converter elements.
It's registered as follows. The propertyEditorUiAlias
matches with the property editor UI that requires the conversions.
Setting labels and descriptions are translated via language files. The following example shows how this is created for the settings on our example field type:
Each different type of extension for Forms uses a different root value:
Data sources - formProviderDataSources
Export types - formProviderExportTypes
Field types - formProviderFieldTypes
Prevalue sources - formProviderPrevalueSources
Recordset actions - formRecordSetActions
Workflows - formProviderWorkflows
The language files are registered with:
Finally, you will need an entry point to your client-side components that will register the manifests with Umbraco's extension registry. For example:
To create custom backoffice components for Umbraco 14, it's recommended to use a front-end build setup using Vite, TypeScript, and Lit. For more information, see the article.