From d7cc626396c5955036f9d3ed10abc9ffc6efec87 Mon Sep 17 00:00:00 2001 From: Stephan Date: Thu, 20 Nov 2014 12:36:07 +0100 Subject: [PATCH] U4-1083 - proper error message on content type alias duplicate --- .../umbraco/create/nodeType.ascx.cs | 53 ++++++++++++++++--- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/create/nodeType.ascx.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/create/nodeType.ascx.cs index 3cb32f364b..4908c80981 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/create/nodeType.ascx.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/create/nodeType.ascx.cs @@ -1,5 +1,6 @@ using System.Globalization; using Umbraco.Core; +using Umbraco.Core.Strings; using Umbraco.Web.UI; using Umbraco.Web; @@ -60,10 +61,34 @@ namespace umbraco.cms.presentation.create.controls e.IsValid = false; } + private bool ValidateAlias(string text) + { + // get the alias - same way we do it in nodetypeTasks.PerformSave() + // ensure it's not empty + var alias = text.ToCleanString(CleanStringType.Alias | CleanStringType.PascalCase, ' '); + if (string.IsNullOrWhiteSpace(alias)) + return false; + + // ensure no content with that alias exists + var existing = ApplicationContext.Current.Services.ContentTypeService.GetContentType(alias); + if (existing != null) + return false; + + // all is ok + return true; + } + protected void sbmt_Click(object sender, EventArgs e) { if (Page.IsValid) { + if (ValidateAlias(rename.Text) == false) + { + BasePage.Current.ClientTools.ShowSpeechBubble(BasePage.speechBubbleIcon.error, "Error", + "Name is empty, invalid, or causing an invalid or duplicate alias."); + return; + } + var createTemplateVal = 0; if (createTemplate.Checked) createTemplateVal = 1; @@ -71,15 +96,27 @@ namespace umbraco.cms.presentation.create.controls // check master type string masterTypeVal = String.IsNullOrEmpty(Request.GetItemAsString("nodeId")) || Request.GetItemAsString("nodeId") == "init" ? masterType.SelectedValue : Request.GetItemAsString("nodeId"); - var returnUrl = LegacyDialogHandler.Create( - new HttpContextWrapper(Context), - BasePage.Current.getUser(), - Request.GetItemAsString("nodeType"), - createTemplateVal, - rename.Text, - int.Parse(masterTypeVal)); + string returnUrl; + try + { + // this *may* throw even if Page.IsValid because of race conditions + returnUrl = LegacyDialogHandler.Create( + new HttpContextWrapper(Context), + BasePage.Current.getUser(), + Request.GetItemAsString("nodeType"), + createTemplateVal, + rename.Text, + int.Parse(masterTypeVal)); + } + catch (Exception ex) + { + // because it's always nicer to show a message to user vs YSOD + BasePage.Current.ClientTools.ShowSpeechBubble(BasePage.speechBubbleIcon.error, "Error", + ex.GetType().FullName + ": " + ex.Message); + return; + } - BasePage.Current.ClientTools + BasePage.Current.ClientTools .ChangeContentFrameUrl(returnUrl) .ChildNodeCreated() .CloseModalWindow();