3
votes

In Umbraco V7 how to show custom error validation message on back office save or publish to user

I have tried following but it shows 'Publishing was cancelled by a 3rd party plugin' not actual error message

void ContentService_Saving(IContentService sender, SaveEventArgs<IContent> e)
{               
     e.Cancel = true;       
     ShowErrorBubble("Error saving item", "Error:duplicate records exists");
}

private static void ShowErrorBubble(string title, string exception)
{           
    try
    {
        umbraco.BasePages.UmbracoEnsuredPage.Current.ClientTools.ShowSpeechBubble(umbraco.BasePages.UmbracoEnsuredPage.speechBubbleIcon.error, title, exception);

    }

    catch (Exception ex)
    {
            //do nothing at the moment, forums suggest we cannot send an error message
    }

}
1
This seems to be a known issue and a fix is due for release in 7.3.0. If you need something before then you could use the workaround described here. - Rob Purcell

1 Answers

1
votes

That's an old snippet you're using. It never worked properly that way anyhow. Try this code instead:

void ContentService_Saving(IContentService sender, SaveEventArgs e)
{                      
     ShowErrorBubble(e, "Error saving item", "Error:duplicate records exists");

}

private static void ShowErrorBubble(SaveEventArgs e, string title, string text)
{           
    try
    {
        e.Messages.Add(new Umbraco.Core.Events.EventMessage(title, text, Umbraco.Core.Events.EventMessageType.Warning));

        e.Cancel = true;
    }

    catch (Exception ex)
    {
            //do nothing at the moment, forums suggest we cannot send an error message
    }

}