Adding A Field Type To Umbraco Forms
C#
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Umbraco.Forms.Core.Enums;
using Umbraco.Forms.Core.Models;
using Umbraco.Forms.Core.Services;
namespace MyFormsExtensions
{
public class MyCustomField : Umbraco.Forms.Core.FieldType
{
public MyCustomField()
{
Id = new Guid("08b8057f-06c9-4ca5-8a42-fd1fc2a46eff"); // Replace this!
Name = "My Custom Field";
Description = "Render a custom text field.";
Icon = "icon-autofill";
DataType = FieldDataType.String;
SortOrder = 10;
SupportsRegex = true;
FieldTypeViewName = "FieldType.MyCustomField.cshtml";
}
// You can do custom validation in here which will occur when the form is submitted.
// Any strings returned will cause the submission to be considered invalid.
// Returning an empty collection of strings will indicate that it's valid to proceed.
public override IEnumerable<string> ValidateField(Form form, Field field, IEnumerable<object> postedValues, HttpContext context, IPlaceholderParsingService placeholderParsingService, IFieldTypeStorage fieldTypeStorage)
{
var returnStrings = new List<string>();
if (!postedValues.Any(value => value.ToString().ToLower().Contains("custom")))
{
returnStrings.Add("You need to include 'custom' in the field!");
}
// Also validate it against the default method (to handle mandatory fields and regular expressions)
return base.ValidateField(form, field, postedValues, context, placeholderParsingService, fieldTypeStorage, returnStrings);
}
}
}Partial view
Read-only partial view
Umbraco backoffice view
Field settings
Settings when inheriting
Backoffice entry rendering
Last updated
Was this helpful?