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(); } }
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?
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.
Sorry, didn't read your code carefully enough. You need to implement the interface correctly, this should work:
[removed code]
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.
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.
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) { }
If you start the debugger it's really easy to find and fix these kind of errors.
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
Validator class
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!