Try our conversational search powered by Generative AI!

Custom error message for ContentArea on a Block model

Vote:
 

I have a container block with just one property, which is required to be populated with an allowed block model. I want to be able to customize the error message if the user tries to publish without adding a block. How can I do this?

I tried this:

Container Block property

    public class AccordionContainerBlock : BlockData
    {
        [CultureSpecific]
        [Required]
        [AllowedTypes(typeof(AccordionBlock))]
        [Display(
            Name = "Accordions",
            Description = "Add individual accordions here.",
            GroupName = SystemTabNames.Content,
            Order = 10)]
        public virtual ContentArea Accordions { get; set; }
    }

Validator class

    public class AccordionContainerValidator : IValidate
    {
        IEnumerable IValidate.Validate(AccordionContainerBlock accordion)
        {
            if (accordion.Accordions.Items.Count < 1)
            {
                return new []{
                    new ValidationError(){
                        ErrorMessage = "Create individual accordions before the group. Then drag them into the accordions block from the assets panel.",
                        PropertyName = "Accordions",
                        Severity = ValidationErrorSeverity.Error,
                        ValidationType = ValidationErrorType.AttributeMatched
                    }
                };
            }

            return Enumerable.Empty();
        }
    }

When I try to publish the container, it says "Object reference not set to an instance of an object." So I'm not sure where I went wrong. I'm a novice at C#.

Thanks!

#152142
Edited, Aug 17, 2016 19:15
Vote:
 

Two things; you're implemention the wrong interface and you also need to do a null check.

public class AccordionContainerValidator : IValidate<AccordionContainerBlock>
{
    IEnumerable IValidate.Validate(AccordionContainerBlock accordion)
    {
        if (accordion.Accordions.Items == null || accordion.Accordions.Items.Count < 1)
        {
            return new []{
                new ValidationError(){
                    ErrorMessage = "Create individual accordions before the group. Then drag them into the accordions block from the assets panel.",
                    PropertyName = "Accordions",
                    Severity = ValidationErrorSeverity.Error,
                    ValidationType = ValidationErrorType.AttributeMatched
                }
            };
        }
 
        return Enumerable.Empty();
    }
}
#152143
Aug 17, 2016 21:28
Vote:
 

OK, thanks for your answer, but it's not working for me. I'm a newbie. When I put in your code, I get errors on the Enumerable.Empty line. "The type arguments for method 'System.Linq.Enumerable.Empty<Tresult>()' cannot be inferred from the usage." I have this within the namespace of my Container block model. What am I missing?

#152146
Edited, Aug 17, 2016 22:14
Vote:
 

If I have a container with accordion blocks already published and then remove them, I do get the custom error. When I first create an accordion group and do not add accordions, that's when I get the error message in my original post.

#152147
Edited, Aug 17, 2016 22:21
Vote:
 


Sorry, didn't read your code carefully enough. You need to implement the interface correctly, this should work:

[removed code]

#152149
Edited, Aug 18, 2016 0:20
Vote:
 

Hmmm why can't we edit the code... here's the correct one:

public class AccordionContainerValidator : IValidate<AccordionContainerBlock>
{
    public IEnumerable<ValidationError> Validate(AccordionContainerBlock instance)
    {
        if (instance.Accordions.Items == null || instance.Accordions.Items.Count < 1)
        {
            yield return new ValidationError
            {
                    ErrorMessage = "Create individual accordions before the group. Then drag them into the accordions block from the assets panel.",
                    PropertyName = "Accordions",
                    Severity = ValidationErrorSeverity.Error,
                    ValidationType = ValidationErrorType.StorageValidation
            };
        }
  
        yield break;
    }
}

"If I have a container with accordion blocks already published and then remove them, I do get the custom error. When I first create an accordion group and do not add accordions, that's when I get the error message in my original post."

That's because you also need to null check instance.Accordians, like in my code. You can't just check whether it has less than 1 item or not, that might throw a null reference exception.

#152150
Edited, Aug 18, 2016 0:23
Vote:
 

I had the null check. I also just copied and pasted your set of code from your last post and it still does the same thing. No worries though, I'll move onto something else. Thanks for your help.

#152246
Aug 22, 2016 14:38
Vote:
 

Glad I could help. I was writing the code just by heart, now when I have access to Visual Studio I see that you also need to null-check instance.Accordions. So:

if (instance.Accordions == null || instance.Accordions.Items == null || instance.Accordions.Items.Count < 1)
{
}
#152247
Aug 22, 2016 14:43
Vote:
 

That did it! Sweet, thank you for sticking with me.

#152248
Aug 22, 2016 14:49
Vote:
 

If you start the debugger it's really easy to find and fix these kind of errors.

#152249
Aug 22, 2016 14:54
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.