Try our conversational search powered by Generative AI!

Number of links in the link item collection

Vote:
 

I want to limit the number of link items in the LinkItemCollection property to 5. Is there any attribute to set it?

[Display(
Name = "Related links",
Description = "Add related links here",
GroupName = SystemTabNames.Content,
Order = 50)]
public virtual LinkItemCollection RelatedLinks { get; set; }

#150606
Jun 23, 2016 16:23
Vote:
 

Hi Anusha,

You can create a custom attribute:

[AttributeUsage(AttributeTargets.Property)]
public sealed class LinkItemCollectionMaxItemsCount : ValidationAttribute
{
	public int Limit { get; }

	public LinkItemCollectionMaxItemsCount(int limit)
	{
		Limit = limit;
	}

	public override bool IsValid(object value)
	{
		return ValidateProperty(value as LinkItemCollection);
	}

	private bool ValidateProperty(LinkItemCollection item)
	{
		return item == null || item.Count <= Limit;
	}
}

And decorate your LinkItemCollection property like this:

[Display(GroupName = Global.GroupNames.SiteSettings, Order = 300)]
[LinkItemCollectionMaxItemsCount(3, ErrorMessage = "Too many items")]
public virtual LinkItemCollection ProductPageLinks { get; set; }

Result:

#150607
Edited, Jun 23, 2016 17:05
Vote:
 

Hi Dejan,

I just extended your solution to support all three list properties that we use these days in EpiServer, maybe it will be helpful for someone to just grab the solution from here.

 [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
    public sealed class MaxItemCount : ValidationAttribute 
    {
        private int Limit { get; }

        public MaxItemCount(int limit)
        {
            Limit = limit;
        }

        public override bool IsValid(object value)
        {
            var contentArea = value as ContentArea;

            if(contentArea != null)
            {
                return ValidateContentArea(contentArea);
            }

            var linkItemCollection = value as LinkItemCollection;

            if (linkItemCollection != null)
            {
                return ValidateLinkItemCollection(linkItemCollection);
            }

            var contentReferenceList = value as IList<ContentReference>;

            if (contentReferenceList != null)
            {
                return ValidateContentReferenceList(contentReferenceList);
            }

            return true;
        }

        private bool ValidateContentReferenceList(IList<ContentReference> contentReferenceList)
        {
            return contentReferenceList == null || contentReferenceList.Count <= Limit;
        }

        private bool ValidateLinkItemCollection(LinkItemCollection linkItemCollection)
        {
            return linkItemCollection == null || linkItemCollection.Count <= Limit;
        }

        private bool ValidateContentArea(ContentArea contentArea)
        {
            return contentArea?.Items == null || contentArea.Items.Count <= Limit;
        }

        public override string FormatErrorMessage(string name)
        {
            return $"Max number of items allowed in {name} is {Limit}";
        }
    }

Cheers,

Bojan

#173395
Dec 22, 2016 13:19
* 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.