Validation on CreatingContent event

Vote:
 

I have an implementation of IValidate which works fine, showing a warning when UrlSegment is > 30 characters. Problem is, the warning is not shown when content is created, only when content is edited and saved. It seems the validation only runs on event SavingContent, not on CreatingContent. This means it is possible to create a new page, give it a loooong name (hence a long UrlSegment), and publish it directly without getting the warning.

I don't want to change it to a validation error instead of validation warning, but I want the warning to show when content is created. Is this possible?

#143949
Feb 03, 2016 17:00
Vote:
 

Try shifting your logic so you validate the length of the name instead if url segment isn't set yet. I seem to recall that UrlSegment is set later? 

Another variant is to automatically handle UrlSegment length by reacting to the event when this segment is created. You set up the event handling in an InitializationModule just like for other events..

[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class UrlSegmentHandlingInitialization : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            UrlSegment.CreatedUrlSegment += UrlSegmentOnCreatedUrlSegment;
        }

        private void UrlSegmentOnCreatedUrlSegment(
            object sender, UrlSegmentEventArgs urlSegmentEventArgs)
        {
            var segment = urlSegmentEventArgs.RoutingSegment.RouteSegment;
...kill hyphens, shorten the urlsegment, to lower etc... }
#143952
Feb 03, 2016 20:14
Vote:
 

It does not matter which property I validate, since the validation logic is not run at all on CreatingContent. 

I don't want to automatically modify the UrlSegment, just to show the warning. Seems impossible!

#143973
Feb 04, 2016 8:53
Vote:
 

Turns out, it is impossible. Eventually I got a reply from EPiServer support: "IValidate is only meant for saving content. This is by design and we have a feature request for adding validation on content creation itself."

I ended up using Daniel's suggestion above, cutting everything >30 characters from UrlSegment.

#145282
Mar 01, 2016 14:46
* 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.