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... }
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!
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.
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?