Unable to set empty string value using SelectionFactory

Ted
Ted
Vote:
 

I have a required property like so (notice how empty strings are allowed):

[Required(AllowEmptyStrings = true)]
[SelectOne(SelectionFactoryType = typeof(ThemeSelectionFactory))]
public virtual string Theme { get; set; }

The selection factory includes both a null option, as well as an empty string option:

public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
{
    return new []
    {
        new SelectItem
        {
            Text = "",
            Value = null
        },
        new SelectItem
        {
            Text = "Default",
            Value = ""
        },
        new SelectItem
        {
            Text = "Purple",
            Value = "primary"
        }
    };
}

The idea is to force the user to explicitly select an option when creating content, but the user may opt to explicitly set the value to an empty string.

However, if the user selects the "Default" option, validation appears to still treat it like null (both properties below use the same pattern):

Of course we can change the "Default" option to have a non-empty value, but that would require refactoring of other parts - and I still think this behavior is odd?

#272386
Edited, Feb 17, 2022 9:29
Vote:
 

I tested with both CMS 11 and 12 and both have same behavior as you described. Without [Required] it works fine with empty string. With [Required] the best I could do is use a space character but as you say this can cause cascading issues. This must be because with empty string or null the row is completely removed from the tblContentProperty table to save space and that causes [Required] check to trigger. So it'd need internal code change to read the parameter to allow empty strings.

using EPiServer.Shell.ObjectEditing;
using System.Collections.Generic;

namespace AlloyTraining.Business.SelectionFactories
{
    public class ThemeSelectionFactory : ISelectionFactory
    {
        public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
        {
            return new List<SelectItem>
            {
                new SelectItem { Value = " ", Text = "None" },
                new SelectItem { Value = "theme1", Text = "Orange" },
                new SelectItem { Value = "theme2", Text = "Purple" },
                new SelectItem { Value = "theme3", Text = "Green" }
            };
        }
    }
}

#272411
Edited, Feb 17, 2022 13:31
Ted - Feb 17, 2022 13:50
Thanks for testing! Do you think this is by design, or an unintentional behavior/bug?
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.