On Page creation of block does not show custom error message when saving

Vote:
 

Hi,

I've a custom validator (MinMaxAttribute) added to a block property.

When I create a new block of that type and save it for the first time, the error message that it needs at least x items is not showing and the block is not added to the ContentArea because of missing rules.

When I create a block following the rules and after saving this block, it will be added.
Deleting one item so it will not pass the rules, the error is shown.
But from here you can publish this block instead of saving this block by creation.

How can I fix this?

My property:

[MinMax(Min = 4, Max = 10)]
[AllowedTypes(new[] { typeof(OnPageNavigationItem) })]
[Display(Name = "Items", Description = "Min. 4, Max. 10 items", Order = 300)]
public virtual ContentArea Items { get; set; }

My custom error attribute:

[AttributeUsage(AttributeTargets.Property)]
    public class MinMaxAttribute : ValidationAttribute
    {
        private static readonly IContentLoader _contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();

        public int Min
        {
            get;
            set;
        }
        public int Max
        {
            get;
            set;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if ((value != null) && !(value is ContentArea) && !(value is LinkItemCollection))
            {
                throw new ValidationException("MinMaxAttribute is intended only for use with ContentArea or LinkItemCollection properties");
            }

            var contentArea = value as ContentArea;
            var linkItemCollection = value as LinkItemCollection;

            var minError = false;
            var maxError = false;

            var min = Min;
            var max = Max;

            if (contentArea != null || linkItemCollection != null)
            {
                var items = contentArea?.Items.Select(c => c.ContentLink).Count() ?? linkItemCollection.Count;

                if (min > 0 && items < min)
                {
                    minError = true;
                }
                if (max > 0 && items > max)
                {
                    maxError = true;
                }
            }
            else if (min > 0)
            {
                minError = true;
            }

            if (minError || maxError)
            {
                if (max > 0 && min > 0)
                {
                    if (min == max)
                    {
                        ErrorMessage = string.Format("Number of items for property '{0}' should be {1}", validationContext.DisplayName, min);
                    }
                    else
                    {
                        ErrorMessage = string.Format("Number of items for property '{0}' should be between {1} and {2}", validationContext.DisplayName, min, max);
                    }
                }
                else if (max > 0)
                {
                    ErrorMessage = string.Format("Number of items for property '{0}' should be maximum {1}", validationContext.DisplayName, max);
                }
                else
                {
                    ErrorMessage = string.Format("Number of items for property '{0}' should be minimum {1}", validationContext.DisplayName, min);
                }

                return new ValidationResult(ErrorMessage, new[] { validationContext.MemberName });
            }
            return null;
        }
    }

Thanks in advance!

Cheers,
Sander

#231308
Nov 24, 2020 12:59
Vote:
 

Sander,

Probably I'm missing something but I do see the error when trying to create a new block from a ContentArea. It's true that the error is not shown straight after creating (which is not a big deal in my opinion) but after you try to save/publish for the first time.

Is it that you have a block type that has ContentArea property inside or it's a page with ContentArea?

Would it be possible for you to be more precise? Ideally you could fork https://github.com/episerver/alloy-mvc-template and create a reproducible environment.

#232958
Dec 02, 2020 12:27
Vote:
 

Hi Bartosz,

Thanks for your response.

It is a content area on a block inside a content area on a page :-)

It is specially when I choose for "create a new block":

After that I choose the block which has a content area in it:

At the end I add 3 or less Items in the block contentarea:

The "Items" property has the [MinMax(Min = 4, Max = 10)] attribute, so by creating this block, it should show the error, but is does not.

Hopefully tou have an idea how to fix this.

Thanks in advance.

Grtz Sander

#232960
Edited, Dec 02, 2020 12:52
Vote:
 

Hi Bartosz,

I still run into this problem and finally created a fork and added my problem: https://github.com/sandervandepas/alloy-mvc-template

Here are the steps to reproduce:

  1. Go to the "About us" (type of "Standard Page") page (http://localhost:56780/EPiServer/CMS/?language=en#context=epi.cms.contentdata:///10) and select the "Content" tab
  2. At the property "Large content area" click on "create a new block"
  3. Choose the block type "OnPageNavigationBlock"
  4. In the modal "New OnPageNavigationBlock" fill in the "Name", "Image" and "Title" properties
  5. At the property "Items" (hover over the name of this property and you see that it needs at least 4 items and has a max of 10 items, set by the MinMax attribute) click on "create a new block"
  6. In the modal "OnPageNavigationItem" fill in the "Name" and "Icon" properties
  7. Save the modal "OnPageNavigationItem" (this new block will be added to the "Items" property on the "OnPageNavigationBlock")
  8. Save the modal "New OnPageNavigationBlock"
  9. See that the new created block is not added to the "Large content area" property of the "Standard Page" because it needs at least 4 items, but no error will be shown.

Please have a look at this and hopefully you will have an answer for this problem.

Thanks in advance!

Kind regards,
Sander

#247411
Jan 21, 2021 19:45
Vote:
 

Hi Sander, I have cross-verified the same validation rules in the alloy sample project and I got an error when we cross the range between 4 to 10 and also getting the error when trying to publish the block with 0 items after creating.

#247434
Jan 21, 2021 23:23
Vote:
 

Hi Sanjay,

Thanks for your reply.

Could you share a screenshot which shows the error?
And in which browser did you see the error?
Maybe it's a browser issue.

Thanks in advance!

Grtz
Sander

#247439
Jan 22, 2021 8:09
Vote:
 

Warning message when placed one new item.

Success:.

#247447
Jan 22, 2021 9:30
Vote:
 

Hi Sanjay,

Ah yeah, that's rigt, but you didn't follow my reproduce steps.
This is when you add the block in normal Edit mode, but it happens when you add the block with inline edit mode, just as you follow my steps.

Could you check this again please?

Thanks in advance!

Grtz
Sander

#247456
Jan 22, 2021 10:36
Sanjay Kumar - Jan 22, 2021 10:50
It's working in normal Edit mode as well, When I removed one block in edit view then also get the error 'Number of items for property 'Items' should be between 4 and 10'

Screen Share:
https://drive.google.com/file/d/1KYKJkK0Os3R7RaZxrDHJe_Dq09DGItvS/view
Sander - Jan 22, 2021 12:51
Yes, I know, but it is about the inline edit mode:

https://snipboard.io/HKWL3B.jpg
https://snipboard.io/7xX1SN.jpg
https://snipboard.io/3E1dKl.jpg
Sanjay Kumar - Jan 22, 2021 18:51
I have tried to replicate the same steps as per provided images could you please confirm is that correct?
https://drive.google.com/file/d/1Xejn9tewQioENPqJEl7-izLsZ-RWWXfD/view

Also, I can't see 'create a new block' link in the page(content area)--> block(content area with MinMax).
Sanjay Kumar - Jan 23, 2021 4:52
If you don't want to upgrade the episerver version then throw the error when your rules failed on 'EventsPublishedContent' event.
Vote:
 

We decided to rethink our approach to so-called `Inline Create`. Hopefully soon we will be able to share more info.

In the meantime please upgrade to:

https://nuget.episerver.com/package/?id=EPiServer.CMS.UI.Core&v=11.32.0

and 

https://nuget.episerver.com/package/?id=EPiServer.Labs.BlockEnhancements&v=0.10.0

Inline Edit was renamed to Quick Edit and moved from Labs to the main product.

Inline Create is only available in nested Content Areas.

I can confirm the bug is there @Sander, will take it into account in the upcoming release.

#247490
Jan 22, 2021 20:08
* 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.