Sitecore WFFM checkbox values on Create Item

898 views Asked by At

I'm using the Create Item save action in a web form. I have a checkbox list that points to a folder containing category items. This is mapped to a Multilist field on the item template that it will create when the user submits the form. But it's passing the checkbox text, not the value, so the multilist for each new item created has bad data in it. Does anyone know how to set the checkbox list to pass the values instead? I'm kind of surprised it's doing this in the first place.

1

There are 1 answers

6
Amir Setoudeh On BEST ANSWER

I haven't had a chance to test this, but theoretically, you could create a new field type that inherits from CheckboxList, and let's say we call it CheckboxListPipedValues. The code would look something like this:

using System.ComponentModel;
using System.Linq;
using System.Text;
using Sitecore.Form.Core.Controls.Data;
using Sitecore.Form.Web.UI.Controls;

public class CheckboxListPipedValues : Sitecore.Form.Web.UI.Controls.CheckboxList
{
    [Browsable(false)]
    public override ControlResult Result
    {
        get
        {
            StringBuilder stringBuilder1 = new StringBuilder();
            var checkedItems = this.InnerListControl.Items.Where(a => a.Selected).ToList();
            var values = string.Join("|", checkedItems.Select(c => c.Value));
            foreach (var item in checkedItems)
            {
                stringBuilder1.AppendFormat("{0}, ", item.Text);
            }
            return new ControlResult(this.ControlName, values, stringBuilder1.ToString(0, (stringBuilder1.Length > 0 ? stringBuilder1.Length - 2 : 0)));
        }
    }
}

In Sitecore, simply go to /sitecore/system/Modules/Web Forms for Marketers/Settings/Field Types/List Types/Checkbox List and duplicate that item. Then change the assembly and class to the new control. Change your form to use the new field, and ensure that the values are mapped properly. Now the output of the value should be a pipe separated list of the values which should work nicely with the multilist field.

EDIT: For MVC, it's the same process, but you'll need to update the MVC Type in the field type item to point to your new class. The code for MVC should look something like this:

using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Form.Core.Controls.Data;
using Sitecore.Forms.Core.Data;
using Sitecore.Forms.Mvc.Data.TypeConverters;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Web.Mvc;


public class CheckBoxListPipedField : CheckBoxListField 
{
    public CheckBoxListPipedField(Item item) : base(item)
    {
    }

    public override ControlResult GetResult()
    {
        var values = new List<string>();
        StringBuilder stringBuilder1 = new StringBuilder();
        if (this.Items != null)
        {
            foreach (SelectListItem selectListItem in
                from item in this.Items
                where item.Selected
                select item)
            {
                values.Add(selectListItem.Value);
                stringBuilder1.AppendFormat("{0}, ", selectListItem.Text);
            }
        }
        var results = string.Join("|", values);
        return new ControlResult(base.ID.ToString(), base.Title, results, stringBuilder1.ToString(0, (stringBuilder1.Length > 0 ? stringBuilder1.Length - 2 : 0)));
    }
}