Limiting items in content area using validation but when we switch between tabs within cms and do some operation there removes validation on that.

Vote:
 

We have used content area validation to have limit on items within content area, but when we switch tab and modify that tab items will remove validation on content area and allow us to add more item in content area and publish the item. Try adding this to hide add new block link from content area https://world.episerver.com/blogs/tuan-do/dates/2017/8/limiting-items-in-a-contentarea/ but its not working and giving error in cms. 

Can you please suggest how to deal with this scenario.

#248614
Feb 15, 2021 4:58
Vote:
 

Try implementing the validation through an class implementing the IValidate interface for the content type instead. This may work for you https://world.episerver.com/documentation/developer-guides/CMS/Content/Validation/ 

#248622
Feb 15, 2021 9:33
Vote:
 

Hi Scott,

Thank you for help on this.

Using IValidate interface this max limit warning will not be removed on switching tabs within cms but IValidate interface will be specific to that particular page if we are using it and can't be generalized(Correct me if I am wrong in understanding this one).

And can we pass the limit from model property? 

In the following code snippet max limit 2 is hard coded, can this be passed from model property?

public class ContentPageValidator : IValidate<MainSheetNavigationBlock>
    {
        public IEnumerable<ValidationError> Validate(MainSheetNavigationBlock page)
        {
            if (page.NavigationItems != null && page.NavigationItems.Count >= 2)
            {
                return new ValidationError[]
                {
                    new ValidationError()
                    {
                         ErrorMessage =  string.Format(
                             CultureInfo.CurrentCulture,
                             "{0} has {1} item(s) and cannot contain more than {2}.",
                            page.GetPropertyName(p => p.NavigationItems),
                            page.NavigationItems.Count,
                             2),
                         PropertyName = page.GetPropertyName(p => p.NavigationItems),
                         Severity = ValidationErrorSeverity.Error,
                         ValidationType = ValidationErrorType.StorageValidation
                    }
                };
            }

            return Enumerable.Empty<ValidationError>();
        }
    }

#248813
Feb 18, 2021 6:30
Scott Reed - Feb 18, 2021 7:42
Yes this is an issue if you want to re-use. Apologies I hadn't properly read the article but that approach seems over complicated whereas normally you'd just create an standard attribute based off the ValidationAttribute class. Paul has a good article here, so I'd trying this version instead

https://world.episerver.com/blogs/paul-gruffydd/dates/2018/10/limiting-the-number-of-items-in-a-content-area-while-supporting-personalisation/

The personalisation aspect is optional but the core code for validation should work
Deepa Puranik - Feb 18, 2021 8:14
Thank you so much, this one works.
We have this in place with IMetadataAware interface, and which was removing warning once tabs in CMS switched.
* 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.