Oct 7, 2018
(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. 

            Name = "Items",
            Description = "Items",
            GroupName = SystemTabNames.Content,
            Order = 30)]
        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


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 community meetup - Sept 29 (virtual + Melbourne)

Super excited to be presenting this Thursday the 29th of September at the Optimizely community meetup. For the full details and RSVP's see the...

Ynze | Sep 27, 2022 | Syndicated blog

Preview multiple Visitor Groups directly while browsing your Optimizely site

Visitor groups are great - it's an easy way to add personalization towards market segments to your site. But it does come with it's own set of...

Allan Thraen | Sep 26, 2022 | Syndicated blog

The Report Center is finally back in Optimizely CMS 12

With Episerver.CMS.UI 12.12.0 the Report Center is finally re-introduced in the core product.

Tomas Hensrud Gulla | Sep 26, 2022 | Syndicated blog

Dynamic Route in ASP.NET Core When MapDynamicControllerRoute Does Not Work

Background Creating one of the add-on for Optimizely I had to deal with challenge to register dynamically route for the API controller. Dynamic rou...

valdis | Sep 25, 2022 | Syndicated blog