Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend Sitecore WFFM field type

I would like to add additional attributes to a WFFM form field type.

The built in field types have attributes at the left of the form designer enter image description here

I would like to add my own section and attribute to this area. Can this be done easily without overwriting the existing field types or hacking with the core code?

I don't really want to have to re create the e.g. Single-Line Text field just to add my own attribute field to it.

like image 306
Carl Thomas Avatar asked Oct 18 '22 22:10

Carl Thomas


1 Answers

Unfortunately the only way to achieve it is by creating a custom Field Type in code that implements an existing Field e.g. Single Line Text. There is no configuration else where to change, you have to add your attributes via code, being able to take and extend the 'core' code is what Sitecore is known for.

But it is really simple to add these attributes and don't have to redevelop every field if you just implement the existing ones. Then simply select your custom Single Line Text from the Type drop down list and see your new attributes..

Implementing the existing Fields will give you everything the Single Line Text does out-of-the-box with its attributes, now you need define the attributes in your new class. The attributes themselves are public properties of your class decorated with visual properties.

For example, I wanted an attribute to hold the file size limit of an FileUpload field, that can be done by adding a public string property;

public class CustomSingleLineText : SingleLineText
{
    private int _fileSizeLimit;

    // Make it editable
    [VisualFieldType(typeof(EditField))]
    // The text display next to the attribute
    [VisualProperty("Max file size limit (MB) :", 5)]
    // The section the attribute appers in
    [VisualCategory("Appearance")]
    public string FileSizeLimit
    {
        get
        {
            return this._fileSizeLimit.ToString();
        }
        set
        {
            int result;
            if (!int.TryParse(value, out result))
                result = 5;
            this._fileSizeLimit = result;
        }
    }

You can then access the attribute value entered by the Content Editor on the submission or even the valiadator by getting it from the Parameters of the FieldItem - FieldItem["Parameters"]

For a full example source see this post;

http://jonathanrobbins.co.uk/2015/10/06/sitecore-marketplace-module-secure-file-upload/

like image 159
Jonathan Robbins Avatar answered Nov 22 '22 17:11

Jonathan Robbins