Let’s Talk

We would love to hear from you. Want to know more about
our services or have any questions? Say Hi!

Special language characters not allowed in Sitecore 10.X Forms | Solution

January 16, 2023
Special language characters not allowed in Sitecore 10.X Forms | Solution
Mitesh Patel
Mitesh Patel
Technical Head
special-language-characters-not-allowed-in-sitecore-10-X-forms-solution

When you create Sitecore Forms, it provides you with the functionality to manage multilingual forms setup.

When creating a new form in Sitecore forms, each control in the form will be created as a separate item for each specific control. But in the case of list fields where each list item is created as a static list item (and list items not rendered from existing items), for the item name Sitecore uses the field value if the field name/label is empty. All good, all good – until you want to use a not-valid character in the field value/label like ë, ä, etc.

Then Sitecore overrides the value to be item name compliant. And therefore, the values in the list control will be changed to item name-compliant values. Sitecore simply removes/deletes the incompliant characters. But is that really what you want? Don’t you want to be able to use language-specific characters to be displayed or stored from your list control?

I’m using Sitecore 10.2 form. I was facing an issue with Radio button List and Check box List.

Here I am sharing the details of the Radio button List and have used the Radio button List field in the ENGLISH language version, Label name Gender, Radiobutton label 1: Male and Radiobutton label 2: Female.

Further, I have created the form in Japanese language and used the existing Radio button List field Radiobutton label 1: 男 and Radiobutton label 2: 女性 , while on click APPLY it takes the value but after on click of SAVE Sitecore form then it's not working there and showed Radiobutton label 1: Unnamed item and Radiobutton label 2: Unnamed item.

Also, in the Check box List and have used the Check Box List field in the ENGLISH language version, Label name Department, Checkbox Label 1: Development. Further has created the same in Japanese language version with existing field Department, Check box Label 1: 発達 , while on click APPLY it takes value and after clicking SAVE Sitecore form, it's not working there and showed Checkbox Label 1: Unnamed the item. And same issue I was facing in Sitecore 10.1 forms.

We analyzed this issue more and identified that there were some issues in this version. We extracted Sitecore dll and found the below 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.

Radio Button:

special-language-characters-not-allowed-in-sitecore-10-X-forms-solution-1 special-language-characters-not-allowed-in-sitecore-10-X-forms-solution-2

Checkbox List:

special-language-characters-not-allowed-in-sitecore-10-X-forms-solution-3 special-language-characters-not-allowed-in-sitecore-10-X-forms-solution-4
Solution:

You can resolve this issue by implementing a custom DataSourceSettingsManager with methods UpdateSettings and UpdateStaticItems as it gets the DisplayName field and sets the value after putting the value through ItemUtil.ProposeValidItemName(). You need to change that so it sets the value as is, without ItemUtil.ProposeValidItemName().DisplayNames can be anything (including Japanese characters) so this was added erroneously.

You then need to set your new and bug-fixed DataSourceSettingsManager in a config-patch file.

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()):

DataSourceSettingsManagerController.cs

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Web; 

using System.Web.Mvc; 

using Sitecore; 

using System.Globalization; 

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 Addact.Web.Areas.Infrastructure 

{ 

    public class DataSourceSettingsManagerController : DataSourceSettingsManager 

    { 

        // GET: Addact/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 UpdateStaticItems( 

      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(); 
            } 
        } 
    } 
} 
                    

AppForm.config:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
<sitecore > 
    <services > 
<register serviceType="Sitecore.ExperienceForms.FieldSettings.IFieldSettingsManager`1[[Sitecore.ExperienceForms.Mvc.Models.ListFieldItemCollection, Sitecore.ExperienceForms.Mvc]], Sitecore.ExperienceForms" > 
<patch:attribute name="implementationType"> My.Assembly.Namespaces.FixedDataSourceSettingsManager, My.Assembly</patch:attribute > 
</register > 
  </services > 
</sitecore > 
</configuration > 
                    

After applying the above solution, the issue was resolved as you can check with the below screenshots, one is the form in ENGLISH LANGUAGE VERSION and the second in JAPANESE LANGUAGE VERSION.

special-language-characters-not-allowed-in-sitecore-10-X-forms-solution-5 special-language-characters-not-allowed-in-sitecore-10-X-forms-solution-6

Now, we are able to add a Japanese text label with help of the above patch and code change.

Conclusion

In the Sitecore Forms, when creating forms, the special language characters in the radio and checkbox list were not allowed. Here in this blog, you can find the steps as to how we came across this issue and how we solved it. You can find the solution to resolve this issue if you come across it.


YOU MAY ALSO LIKE