Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Keith Boynton 19 posts 61 karma points
    Dec 08, 2020 @ 10:37
    Keith Boynton
    0

    Content item is lost due to onsave validation when using scheduled publish

    I'm handling validation of a blog article to make sure an associated media image has alt text. I'm using the ContentSaving event as below:

                    // Make sure there is alt text on the main image
                    var mainImage = blogArticleEntity.GetValue("mainImage");
                    if (mainImage != null)
                    {
                        var mainImageUdi = Udi.Parse((string)mainImage);
                        using (var contextReference = _contextFactory.EnsureUmbracoContext())
                        {
                            var mainImageMediaItem = contextReference.UmbracoContext.Media.GetById(mainImageUdi);
                            if (mainImageMediaItem != null)
                            {
                                var mainImageAltText = mainImageMediaItem.Value<string>("alternativeText");
                                if (string.IsNullOrWhiteSpace(mainImageAltText))
                                {
                                    e.CancelOperation(new EventMessage("Main Image", "The main image is missing alternative text", messageType: EventMessageType.Error));
                                    break;
                                }
                            }
                        }
                    }
    

    That seems to be all fine and works well when trying to save the item. The error message is displayed and the content isn't saved. You can resolve the validation by editing the image, adding alt text and then saving.

    All fine and dandy UNTIL a user uses scheduled publish instead of save...

    It appears to work fine, you schedule the publish and the error message is displayed as expected.

    The problem comes when you try to add the alt text to the image to fix the validation error. You save the media item with the added alt text and the UI tries to reload the content item returning a 404 error from the GetById controller action and your work in progress is lost.

    The difference I've noticed between using save and using scheduled publish is when you "Save" the media item after having saved you get a "discard changes" message which lets you cancel the reload. But, you don't get that prompt when a user has used the scheduled publish instead of the Save and so the work in progress is lost.

    Would you guys consider this a bug or is there some other way I should be handling validation of alt text on an associated media item?

Please Sign in or register to post replies

Write your reply to:

Draft