Try our conversational search powered by Generative AI!

Aniket
Oct 7, 2018
  6216
(3 votes)

Max & Min element validation for Content Area or Link Collection

Reccently we had a business requirement for setting minimum & maximum limit for blocks in Content Areas. While we can educate the content authors to set the correct number of blocks in the content area it's always recommended to add validation within the CMS to avoid human errors.

Here's the code to to set the maximum number of blocks in the content area and Link Item Collection. 

/// <summary>
    /// Sets the maximum element count in a linkcollection, a content area - or any other type of collection.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class MaxElementsAttribute : ValidationAttribute, IMetadataAware
    {
        public int MaxCount { get; set; }

        public void OnMetadataCreated(ModelMetadata metadata)
        {
            //TODO: Use to disable editor drag and drop at a certain point.
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value == null)
            {
                return null;
            }
            if (value is LinkItemCollection)
            {
                if ((value as LinkItemCollection).Count > MaxCount)
                {
                    return new ValidationResult("This field exceeds the maximum limit of " + MaxCount + " items");
                }
            }
            else if (value is ContentArea)
            {
                if ((value as ContentArea).Count > MaxCount)
                {
                    return new ValidationResult("This field exceeds the maximum limit of " + MaxCount + " items");
                }
            }

            return null;
        }

        public MaxElementsAttribute(int MaxElementsInList)
        {
            this.MaxCount = MaxElementsInList;
        }
    }

On the Content Area field (or Link Item Collection) set the MaxElements attribute as shown below. 

[Display(
            Name = "Items",
            Description = "Items",
            GroupName = SystemTabNames.Content,
            Order = 30)]
        [MaxElements(25)]
        public virtual ContentArea Items { get; set; }

You can use the same logic for setting the minimum number of elements as well. 



Happy coding :)

Oct 07, 2018

Comments

Antti Alasvuo
Antti Alasvuo Oct 9, 2018 05:49 AM

Hi Aniket, first thanks for sharing this.

You should use the 'is' pattern matching in the code, see the docs. Now you first test is the value of some type (casting) and then if it was successful then the code casts again the object.

The XML comment says that it handles 'any other type of collection' but there is no implementation for collections in the code you have provided.

Please login to comment.
Latest blogs
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog

The A/A Test: What You Need to Know

Sure, we all know what an A/B test can do. But what is an A/A test? How is it different? With an A/B test, we know that we can take a webpage (our...

Lindsey Rogers | Apr 15, 2024

.Net Core Timezone ID's Windows vs Linux

Hey all, First post here and I would like to talk about Timezone ID's and How Windows and Linux systems use different IDs. We currently run a .NET...

sheider | Apr 15, 2024