CMS 12, any way to display validation warning on content events


Example I would like to validate state of blocks in a contentarea (for example are they all published) and show warnings if nessecarry on LoadedContent (not on SavedContent or PublishingContent ... like IValidate<TContent>)

Jan 12, 2022 13:37

Hi Palle,

I don't think this would be possible, however you may perfer to hook in to the 'SavingContent' event. Whenever you make changes Optimizely will save the content automatically in the background. 

Attaching and event listener to this event will allow you to validate the content and raise a message to the user.

The following is for creating content however the same could be applied to the savingcontent event.

private void ContentEvents_CreatingContent(object sender, EPiServer.ContentEventArgs e)
            if (e.Content is ImageFile imageFile && imageFile.BinaryData != null)
                using (var blobData = imageFile.BinaryData.OpenRead())
                    int.TryParse(ConfigurationManager.AppSettings["ImageMaxFileSize"], out var imageMaxFileSize);

                    if (imageMaxFileSize <= 0)
                        imageMaxFileSize = 1024;

                    if (blobData.Length > imageMaxFileSize)
                        e.CancelAction = true;
                        e.CancelReason = string.Format(new FilesizeFormatProvider(), "Filesize exceeds the maximum allowed {0:fs}", imageMaxFileSize);
Jan 13, 2022 13:25
Palle Mertz - - Jan 13, 2022 14:35
Thanks but it unfortunately does not fit my scenario.
CancelAction will cancel the action which is not what I need ... like (Severity = ValidationErrorSeverity.Error)
Palle Mertz - - Jan 13, 2022 14:37
Oh and by the way actually cancelling the action (for instance on publish) comes with some other issues which will make it a bad solution:
Paul McGann - Jan 14, 2022 14:18
Have you looked at using IContentSaveValidate instead of IValidate? Again this performs the validation when saving. You could then check the contents of the ContentArea and see if they are published and if so then fail the validation.

new ValidationError()
ErrorMessage = "This is a warning",
PropertyName = page.GetPropertyName(property => property .PageName),
Severity = ValidationErrorSeverity.Error,
ValidationType = ValidationErrorType.AttributeMatched
Palle Mertz - - Jan 14, 2022 16:40
Well fyi ...IValidate is also run on Save, so it really doesn't matter.

The problem is that warning only errors will really never be shown when you Publish ... only on Save. So you have to change something on the page for it to trigger and be shown. But I would like to validate the state of the blocks meaning you do not really change anything on the page only the blocks are changed.

And if I block publish of the page - ValidationErrorSeverity.Error- (because block states are invalid) another problem arises. If you go into the block and fix it the "Invalid state" on the page is not cleared (when you go back or even change forth and back to the page) until you refresh you browser. As I state in the commet above with a link to another thread I posted.

I have decided now to handle Published events and run the IValidate validator inside and post notifications to the user instead if any errors (the editors will then at least get notified).
Paul McGann - Jan 14, 2022 17:58
Which publish events are you attaching to, the publishing of the page or the block? Are you using the IValidationService to perform the validation?
Palle Mertz - - Jan 27, 2022 7:05
Right now I am attaching to Published event on the page. I am using the my IValidator
* 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.