Adding a Magic String Format Function
This builds on the "adding a type to the provider model" chapter
Umbraco Forms Magic Strings 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.
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.
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:
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:
Last updated