Umbraco Forms
CMSCloudHeartcoreDXP
13.latest (LTS)
13.latest (LTS)
  • Umbraco Forms Documentation
  • Legacy Documentation
  • Release Notes
  • Installation
    • Installing Umbraco Forms
    • Licensing
  • Upgrading
    • Upgrading Umbraco Forms
    • Version Specific Upgrade Notes
    • Migration IDs
  • Editor
    • Creating a Form - The basics
      • Form Settings
      • Form Advanced Options
      • Form Information
      • Overview Of The Field Types
        • Date
        • File Upload
        • reCAPTCHA V2
        • reCAPTCHA V3
      • Setting-up Conditional Logic on Fields
    • Attaching Workflows
      • Workflow Types
    • Viewing And Exporting Entries
    • Defining And Attaching Prevalue Sources
      • Prevalue Source Types Overview
  • Developer
    • Preparing Your Frontend
    • Rendering Forms
    • Rendering Forms Scripts
    • Themes
    • Custom Markup
    • Email Templates
    • Working With Record Data
    • Umbraco Forms in the Database
    • Extending
      • Adding A Type To The Provider Model
        • Setting Types
      • Adding A Field Type To Umbraco Forms
        • Excluding a built-in field
      • Adding A Prevalue Source Type To Umbraco Forms
      • Adding A Workflow Type To Umbraco Forms
      • Adding An Export Type To Umbraco Forms
      • Adding a Magic String Format Function
      • Adding A Server-Side Notification Handler To Umbraco Forms
      • Adding a Validation Pattern
      • Customize Default Fields and Workflows For a Form
    • Configuration
      • Forms Provider Type Details
    • Webhooks
    • Security
    • Magic Strings
    • Health Checks
      • Apply keys and indexes
      • Apply keys and indexes for forms in the database
    • Localization
    • Content Apps
    • Headless/AJAX Forms
    • Block List Filters
    • Field Types
    • Storing Prevalue Text Files With IPreValueTextFileStorage
  • Tutorials
    • Overview
    • Creating a Contact Form
Powered by GitBook
On this page
  • Creating a custom format function
  • Registering the custom format function
  • Using the custom format function

Was this helpful?

Edit on GitHub
Export as PDF
  1. Developer
  2. Extending

Adding a Magic String Format Function

PreviousAdding An Export Type To Umbraco FormsNextAdding A Server-Side Notification Handler To Umbraco Forms

Last updated 1 year ago

Was this helpful?

This builds on the "" chapter

Umbraco Forms can be used to replace placeholders within form elements with values from different sources. Sources include the HTTP request or the Umbraco page where the form is hosted.

These values can be formatted using .

Filter functions for common operations such as truncating a string or formatting a date or number are provided. It's also possible to create custom ones in code.

Creating a custom format function

To create a custom format function, create a class that implements IParsedPlaceholderFormatter.

The FunctionName property provides the name of the function that will be used within the form's magic string.

The FormatValue property parses the provided value and arguments and returns the formatted value as a string.

The following example shows the implementation of a function that bounds an integer value. It takes two arguments, a minimum and maximum value. If the value read from the magic string source is numeric, and fits within the two bounds, it is returned. Otherwise, either the minimum or maximum value is returned depending on whether the value is lower or higher than the bounds respectively.

using System.Globalization;
using Umbraco.Forms.Core.Interfaces;

namespace Umbraco.Forms.Core.Providers.ParsedPlacholderFormatters
{
    public class BoundNumber : IParsedPlaceholderFormatter
    {
        public string FunctionName => "bound";

        public string FormatValue(string value, string[] args)
        {
            if (args.Length != 2)
            {
                return value;
            }

            if (!int.TryParse(args[0], out var min) || !int.TryParse(args[1], out var max))
            {
                return value;
            }

            if (int.TryParse(value, out int valueAsInteger) ||
                int.TryParse(value, NumberStyles.None, CultureInfo.InvariantCulture, out valueAsInteger))
            {
                if (valueAsInteger < min)
                {
                    return min.ToString();
                }

                if (valueAsInteger > max)
                {
                    return max.ToString();
                }

                return valueAsInteger.ToString();
            }

            return value;
        }
    }
}

Registering the custom format function

As with other provider types, the custom function needs to be registered. An example registration using the IUmbracoBuilder is shown below:

public static IUmbracoBuilder AddCustomProviders(this IUmbracoBuilder builder)
{
    builder.FormsParsedPlaceholderFormatters()
        .Add<BoundNumber>();
    return builder;
}

Using the custom format function

The format function can be used within a form's magic string in the same way as the ones provided with Umbraco Forms.

For the example provided, it would be used like this:

[#field | bound: 1: 10]
adding a type to the provider model
Magic Strings
filter functions