Join us this Friday for AI in Action at the Virtual Happy Hour! This free virtual event is open to all—enroll now on Academy and don’t miss out.

 

Allan Thraen
Nov 27, 2017
  6617
(2 votes)

Limit number of elements in a LinkItemCollection or ContentArea

A colleague asked me today how to ensure that editors don't add too many elements in a LinkItemCollection or ContentArea - since too many could potentially break the design. I figured a simple validation attribute could do the trick - and sure enough, it can. I figured I'd share it here as it's a nice example of how to do custom validations of property values. I could have extended it even further to also ensure that the front-end code wouldn't even allow for the editor to try and drop an element if there's already too many - but due to time restraints and my limited dojo-skills, I simply left that part as a fun TODO for all of you out there. If you have that part, feel free to share in the comments...

Here is the main class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.ModelBinding;
using EPiServer.Core;
using EPiServer.ServiceLocation;
using EPiServer.Shell.ObjectEditing;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using EPiServer.SpecializedProperties;

namespace ExperimentsValidationAttributes
{
    /// <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 : System.ComponentModel.DataAnnotations.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("Too many Link Items in the collection. Maximum is " + MaxCount);
                }
            } else if(value is ContentArea)
            {
                if((value as ContentArea).Count>MaxCount)
                {
                    return new ValidationResult("Too many content items in content area. Maximum is " + MaxCount);
                }
            } 

            return null;
        }

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

And of course it's straightforward to use:

        [MaxElements(3)]
        public virtual LinkItemCollection Links { get; set; }

        [Display(
            GroupName = SystemTabNames.Content,
            Order = 320)]
        [MaxElements(5)]
        public virtual ContentArea MainContentArea { get; set; }
Nov 27, 2017

Comments

valdis
valdis Nov 28, 2017 10:16 AM

cool. also, similar - if you are using Bootstrap and want to notify editors that too much items in that content area might blow up something - you can use very similar validator. https://github.com/valdisiljuconoks/EPiBootstrapArea/blob/master/README.md#validate-item-count

Robert Runge
Robert Runge Nov 29, 2017 10:05 AM

Nice. Does this take into consideration the use of visitor groups?

I might want maximum five elements per visitor group - and not in all.

Please login to comment.
Latest blogs
Best Bets, synonyms and wildcard queries

When using a wildcard in your search query like ‘search.For(query+”*”)’ or when you used the reversed method suggested in the Breaking changes...

Jeroen Stemerdink | Jan 28, 2025 | Syndicated blog

Content statistics Blazor component

Another week and another MudBlazor component to explore. I wanted to test the charts components so I created a small Blazor component that displays...

Per Nergård (MVP) | Jan 28, 2025

Referencing Page Specific Blocks with ISelectionFactory

A content modeling exercise got me thinking about reuse of page-specific content. It turns out that Optimizely has some good tools to handle this...

Nicholas Sideras | Jan 28, 2025 | Syndicated blog

Further Enhancing Production Database Scalability in DXP Cloud Services

About a year ago we announced that we are improving the scalability of SQL databases in DXP Cloud Services , focusing first on non-production...

Anna Pleshakova | Jan 27, 2025

Automatic Alt-Text for Images with AI Assistant for Optimizely

Creating accessible and user-friendly content has never been more critical. For websites, ensuring that images include descriptive alt-text is an...

Luc Gosso (MVP) | Jan 26, 2025 | Syndicated blog

Optimizely CMS easy RSS feed integration library

As I've mentioned in my  previous blog post , while I was developing the Optimizely version of my blog, I tried to look for a library that could...

David Drouin-Prince | Jan 25, 2025 | Syndicated blog