Limit the Number of Items Allowed in an Episerver PropertyList<T>

I have to admit, I'm a huge fan of the Episerver PropertyList. Ever since I discovered it, I have been using it a ton. On a recent project, there was a requirement I had to build that was perfectly suited for the PropertyList<T>. The one catch was that I needed to limit the number of repeatable items allowed in the list. It turns out that it was easier than you might think, simply by creating a custom attribute that derives from ValidationAttribute and implements a few of it's functions. Here is the full code for the custom attribute:

    public class MaxRepeatableItemsAttribute : ValidationAttribute
        private int _max;

        public MaxRepeatableItemsAttribute(int max)
            _max = max;

        public override bool IsValid(object value)
            string error = $"restricted to {_max} items!";
            var collection = value as ICollection;

            if (collection?.Count > _max)
                ErrorMessage = error;

                return false;

            return true;

        /// <summary>
        /// Validation result
        /// </summary>
        /// <param name="value"></param>
        /// <param name="validationContext"></param>
        /// <returns></returns>
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
            var result = base.IsValid(value, validationContext);

            if (!string.IsNullOrWhiteSpace(result?.ErrorMessage))
                result.ErrorMessage = $"{validationContext.DisplayName} {ErrorMessage}";

            return result;

As you can see, the code is pretty straight-forward. A few key things to note: First, make sure your attribute class derives from ValidationAttribute. Also, make sure you decorate your class with the [AttributeUsage(AttributeTargets.Property)] attribute. The magic happens inside the IsValid function. We take our object and cast it to an ICollection, then simply use it's Count function to get a value to compare to the int variable max that gets passed to the attribute. As my son Lew would say, easy, peasy, lemon squeezy! Now, all we have to do is add our custom attribute to a PropertyList<T> property on a model:

    Name = "Rotating Banner Slides",
    GroupName = SystemTabNames.Content,
    Order = 5)]
[EditorDescriptor(EditorDescriptorType = typeof(CollectionEditorDescriptor<SlideItem>))]
public virtual IList<SlideItem> Carousel { get; set; }

Now, if an editor tries to save a page or block that has a PropertyList with a limit set, Episerver won't allow it and will display your error message. One thing to keep in mind, as of the date of this post, the Episerver PropertyList<T> is still listed as a pre-release API, which means that it could change at some point before it is officially released. That being said, I think this code will still hold up regardless. If you have any questions or comments, I'd love to hear from you! Please join the conversation below.

0 comment(s) in response to Limit the Number of Items Allowed in an Episerver PropertyList<T>

Have a comment?