November Happy Hour will be moved to Thursday December 5th.

Can we limit the number of options user can select in checkboxes created by SelectMany attribute in edit mode?

Vote:
 

Can we limit the number of options user can select in checkboxes created by SelectMany attribute in edit mode? I have created the checkboxes as explained in https://world.episerver.com/documentation/developer-guides/CMS/Content/Properties/built-in-property-types/Single-or-multiple-list-options/.

#201975
Mar 11, 2019 8:49
Vote:
 

Hi Bilash

I have not tried it but you might be able to use an IValidator<T>implementation on the form element for this: https://world.episerver.com/documentation/developer-guides/CMS/Content/Validation/ 

David

#201982
Mar 11, 2019 12:22
Vote:
 

Hi,

I have tried the IValidate<T> and it will not work as your requirement for a property validation. What will happen is, publish button will not be visible even you update your property for a valid value. Error will keep showing. I think you should use ValidationAttribute. I can share you a code example if use need to understand the implementation of ValidationAttribute.

/Praful

#202020
Edited, Mar 12, 2019 9:58
Vote:
 

Hi,

I have created the [MaxElements] attribute. You can try that.

Property on the block

  [Display(
            Name = "Cards",
            Description = "Cards for block",
            GroupName = CmsGroupDefinitions.Content,
            Order = 300)]
        [Required]
        [MaxElements(4)]
        public virtual IList<CardItem> Cards { get; set; }

Then create a class

namespace ABC.Application.Foundation.Attributes
{
    using System;
    using System.ComponentModel.DataAnnotations;
    using System.Web.ModelBinding;
    using EPiServer.Core;
    using EPiServer.SpecializedProperties;

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class MaxElementsAttribute : ValidationAttribute, IMetadataAware
    {
        public MaxElementsAttribute(int maxElementsInList)
        {
            this.MaxCount = maxElementsInList;
        }

        public int MaxCount { get; set; }

        public void OnMetadataCreated(ModelMetadata metadata)
        {
        }

        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;
        }
    }
}

Thanks

Ravindra

#204578
Jun 10, 2019 10:56
* 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.