1

I was trying to add static List Items for a Dropdown list field in Sitecore Form, the label contains a comma, after clicking the Apply button I can see the value still stays as shown here:

enter image description here

I can also see the comma shown in the Default chosen list item dropdown:

enter image description here

But that comma will be stripped after I click the form's Save button. Is this a bug or by design? how do I enter data that contains comma for list item label?

1 Answer 1

1

This is a known issue. Special characters and nonbasic Latin alphabet are removed in Sitecore Forms after saving changes.

Other Solution:

The issue is that Sitecore uses the ItemUtil.ProposeValidItemName for creating the display name for the item. Making Sitecore Forms compliant with these characters, then instead of using the ProposeValidItemName when creating the items, we will simply use the listFieldItem.Text or listFieldItem.Value.

As an example we replace the ItemUtil.ProposeValidItemName(listFieldItem.Text) : ItemUtil.ProposeValidItemName(listFieldItem.Value)" with "listFieldItem.Text : listFieldItem.Value

(from Sitecore.ExperienceForms.Mvc.DataSource.DataSourceSettingsManager.UpdateStaticItems()):

using System;

using System.Collections.Generic;

using System.Globalization;

using Sitecore;

using Sitecore.Data;

using Sitecore.Data.Items;

using Sitecore.Diagnostics;

using Sitecore.ExperienceForms.Extensions;

using Sitecore.ExperienceForms.FieldSettings;

using Sitecore.ExperienceForms.Mvc.Constants;

using Sitecore.ExperienceForms.Mvc.DataSource;

using Sitecore.ExperienceForms.Mvc.Models;

 

namespace Norican.Infrastructure.Web.Sc.Forms

{

    public class DataSourceSettingsManagerWithDisplayName : DataSourceSettingsManager

    {

        protected override void UpdateSettings(

            ListFieldItemCollection settings,

            Item fieldSettingsItem,

            FieldSettingsContext settingsContext)

        {

            Assert.ArgumentNotNull((object)settings, nameof(settings));

            Assert.ArgumentNotNull((object)fieldSettingsItem, nameof(fieldSettingsItem));

            Assert.ArgumentNotNull((object)settingsContext, nameof(settingsContext));

            if (settingsContext.FieldItem == null)

                return;

            if (MainUtil.GetBool(settingsContext.FieldItem.Fields["Is Dynamic"]?.Value, false))

            {

                foreach (Item child in fieldSettingsItem.Children)

                    child.Recycle();

            }

            else

            {

                string displayFieldName = settingsContext.FieldItem.Fields["Display Field Name"]?.Value;

                string valueFieldName = settingsContext.FieldItem.Fields["Value Field Name"]?.Value;

                this.UpdateStaticItemsInternal((List<ListFieldItem>)settings, fieldSettingsItem, displayFieldName, valueFieldName);

            }

        }

 

        protected void UpdateStaticItemsInternal(

      List<ListFieldItem> items,

      Item fieldSettingsItem,

      string displayFieldName,

      string valueFieldName)

        {

            Assert.ArgumentNotNull((object)items, nameof(items));

            Assert.ArgumentNotNull((object)fieldSettingsItem, nameof(fieldSettingsItem));

            for (int index = 0; index < items.Count; ++index)

            {

                ListFieldItem listFieldItem = items[index];

                if (string.IsNullOrEmpty(listFieldItem.Value))

                {

                    listFieldItem.ItemId = "";

                }

                else

                {

                    string str = ItemUtil.ProposeValidItemName(listFieldItem.Value);

                    Item obj = ID.IsID(listFieldItem.ItemId) ? fieldSettingsItem.Database.GetItem(listFieldItem.ItemId, fieldSettingsItem.Language) : (Item)null;

                    if (obj == null)

                        obj = this.AddItem(str, fieldSettingsItem, new TemplateID(TemplateIds.ListFieldTemplateId));

                    else if (!obj.Axes.IsDescendantOf(fieldSettingsItem))

                        obj = obj.CopyTo(fieldSettingsItem, str);

                    listFieldItem.ItemId = obj?.ID.ToString() ?? string.Empty;

                    if (obj != null)

                    {

                        if (string.IsNullOrEmpty(valueFieldName))

                            valueFieldName = obj.Template.IsBasedOnTemplate(TemplateIds.ListFieldTemplateId) ? "Value" : "__ItemName";

                        obj.Editing.BeginEdit();

                        obj.Name = str;

                        obj.Fields[valueFieldName]?.SetValue(listFieldItem.Value, false);

 

                        /* Use item text or item value as displayName.

                           Original Sitecore code uses ItemUtil.ProposeValidItemName:

                           ... ItemUtil.ProposeValidItemName(listFieldItem.Text) : ItemUtil.ProposeValidItemName(listFieldItem.Value); */

                        (string.IsNullOrEmpty(displayFieldName) || obj.Fields[displayFieldName] == null ? obj.Fields[FieldIDs.DisplayName] : obj.Fields[displayFieldName])?.

                            SetValue(!string.IsNullOrEmpty(listFieldItem.Text) ? listFieldItem.Text : listFieldItem.Value, false);

                        

                        obj.Fields[FieldIDs.Sortorder]?.SetValue((index * 100).ToString((IFormatProvider)CultureInfo.InvariantCulture), false);

                        obj.Editing.EndEdit();

                    }

                }

            }

            foreach (Item child1 in fieldSettingsItem.Children)

            {

                Item child = child1;

                if (!items.Exists((Predicate<ListFieldItem>)(li => child.ID.ToString().Equals(li.ItemId, StringComparison.OrdinalIgnoreCase))))

                    child.Delete();

            }

        }

    }

}

And adding it to the config file:

<sitecore>

  <services>

      <register patch:instead="register[@implementationType='Sitecore.ExperienceForms.Mvc.DataSource.DataSourceSettingsManager, Sitecore.ExperienceForms.Mvc']" serviceType="Sitecore.ExperienceForms.FieldSettings.IFieldSettingsManager`1[[Sitecore.ExperienceForms.Mvc.Models.ListFieldItemCollection, Sitecore.ExperienceForms.Mvc]], Sitecore.ExperienceForms" implementationType="Norican.Infrastructure.Web.Sc.Forms.DataSourceSettingsManagerWithDisplayName, Norican.Infrastructure" lifetime="Transient" />

    </services>

</sitecore>

Credit: Sitecore 10 Forms and special characters in list control

Hope it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.