Sitecore WFFM Create Custom Field

Creating custom WFFM Form field is simple and only requires three steps. In the following example, I just want to create a new Singleline field suggesting the country, where the visitor is located.

First you need to create a WFFM Field using the “/sitecore/templates/Web Forms for Marketers/Field Type” template. The new WFFM Field should be located at “/sitecore/system/Modules/Web Forms for Marketers/Settings/Field Types” in the Master database. In this case, I added at “/Custom”. Fill in the Assembly, the Class and the MVC type fields.



After creating the new Custom WFFM Field item, the field is visible at the Field Type dropdown list in the Form Designer:


Implement the codings. inherit from Sitecore.Form.Web.UI.Controls.SingleLineText, and override OnInit(EventArgs e):

namespace MyExtensions.Logic.FieldTypes.WFFM
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Text;
    using System.Web.UI.WebControls;
    using DSV.Cms.Logic.Helpers;

    using Sitecore.Data.Items;

    public class CountrySingleLineText : Sitecore.Form.Web.UI.Controls.SingleLineText
    {
        protected override void OnInit(EventArgs e)
        {
            this.textbox.CssClass = "scfSingleLineTextBox country";
            this.help.CssClass = "scfSingleLineTextUsefulInfo";
            this.generalPanel.CssClass = "scfSingleLineGeneralPanel";
            this.title.CssClass = "scfSingleLineTextLabel";
            this.textbox.TextMode = TextBoxMode.SingleLine;
            this.Controls.AddAt(0, this.generalPanel);
            this.Controls.AddAt(0, this.title);
            this.generalPanel.Controls.AddAt(0, this.help);
            this.generalPanel.Controls.AddAt(0, this.textbox);
            this.textbox.Attributes.Add("type", "text");

            this.textbox.Text = LanguageSuggestion();

            StringBuilder sbCountries = new StringBuilder();
            List countryNameList = GetCountryNames();
            sbCountries.Append("[");
            int index = 1;
            foreach (string country in countryNameList)
            {
                sbCountries.Append(string.Format("\"{0}\"", country));
                if (index != countryNameList.Count)
                {
                    sbCountries.Append(",");
                }
                index ++;
            }
            sbCountries.Append("]");
            this.textbox.Attributes.Add("data-countries", sbCountries.ToString());
        }

        private string LanguageSuggestion()
        {
            // Do your language look up
            return countryCode;
        }


        protected List GetCountryNames()
        {
            //Do your listing of the Country Item values and add them to list
            return countryItemList;
        }
    }
}