0
votes

I have a list of Translations -> id, isdefault, language and content. I want to validate that 1. when user save, it should check if the list already contains same id or language? 2. if user checked IsDefault, it should check the list that if there is already isdefault selected for any other translations. if yes then it should show the message that please un-select the IsDefault.

Please suggest.

1

1 Answers

1
votes

Can be handled with ContentService in ApplicationEventHandler inheritor class, using Saving event, like the following:

using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Services;

namespace CustomerHomePage.Core.EventHandlers
{
    public class ApplicationHandler : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContentService.Saving += ContentServiceOnSaving;
        }

        private void ContentServiceOnSaving(IContentService sender, SaveEventArgs<IContent> saveEventArgs)
        {
            // Here should be any logic during saving of changes in any nodes
        }
    }
}

Get the ancestor of the sender and find your list of Translation, then just check all children elements value IsDefault. If you want to cancel saving and display some message do the following:

        if (e.CanCancel)
        {
            e.Cancel = true;
            e.CancelOperation(new EventMessage("Category name", "You message here", EventMessageType.Error));
        }