From f6db9d918929bb4efab5fa30848d218956bae2d1 Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 24 Sep 2013 15:00:43 +1000 Subject: [PATCH] Updates textbox/textarea property editors to show field level validation for the regex/required server validation. --- .../propertyeditors/textarea/textarea.html | 3 +- .../propertyeditors/textbox/textbox.html | 3 +- .../Filters/ContentItemValidationHelper.cs | 58 +++++++++++-------- 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/views/propertyeditors/textarea/textarea.html b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/textarea/textarea.html index cfe99ebccc..7d110df737 100644 --- a/src/Umbraco.Web.UI.Client/src/views/propertyeditors/textarea/textarea.html +++ b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/textarea/textarea.html @@ -1 +1,2 @@ - \ No newline at end of file + + \ No newline at end of file diff --git a/src/Umbraco.Web.UI.Client/src/views/propertyeditors/textbox/textbox.html b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/textbox/textbox.html index 2361896bac..b17c59dea7 100644 --- a/src/Umbraco.Web.UI.Client/src/views/propertyeditors/textbox/textbox.html +++ b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/textbox/textbox.html @@ -1 +1,2 @@ - \ No newline at end of file + + \ No newline at end of file diff --git a/src/Umbraco.Web/WebApi/Filters/ContentItemValidationHelper.cs b/src/Umbraco.Web/WebApi/Filters/ContentItemValidationHelper.cs index 2b53daec0f..adc12b58d7 100644 --- a/src/Umbraco.Web/WebApi/Filters/ContentItemValidationHelper.cs +++ b/src/Umbraco.Web/WebApi/Filters/ContentItemValidationHelper.cs @@ -1,8 +1,10 @@ using System; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http.Controllers; +using System.Web.Http.ModelBinding; using Umbraco.Core; using Umbraco.Core.Logging; using Umbraco.Core.Models; @@ -133,27 +135,9 @@ namespace Umbraco.Web.WebApi.Filters // * Combine the preValues with the overridden values stored with the document type property (but how to combine?) // * Or, pass in the overridden values stored with the doc type property separately - foreach (var v in editor.ValueEditor.Validators) + foreach (var result in editor.ValueEditor.Validators.SelectMany(v => v.Validate(postedValue, preValues, editor))) { - foreach (var result in v.Validate(postedValue, preValues, editor)) - { - //if there are no member names supplied then we assume that the validation message is for the overall property - // not a sub field on the property editor - if (!result.MemberNames.Any()) - { - //add a model state error for the entire property - actionContext.ModelState.AddModelError(string.Format("{0}.{1}", "Properties", p.Alias), result.ErrorMessage); - } - else - { - //there's assigned field names so we'll combine the field name with the property name - // so that we can try to match it up to a real sub field of this editor - foreach (var field in result.MemberNames) - { - actionContext.ModelState.AddModelError(string.Format("{0}.{1}.{2}", "Properties", p.Alias, field), result.ErrorMessage); - } - } - } + AddError(actionContext.ModelState, result, p.Alias); } //Now we need to validate the property based on the PropertyType validation (i.e. regex and required) @@ -162,22 +146,46 @@ namespace Umbraco.Web.WebApi.Filters { foreach (var result in p.PropertyEditor.ValueEditor.RequiredValidator.Validate(postedValue, "", preValues, editor)) { - //add a model state error for the entire property - actionContext.ModelState.AddModelError(string.Format("{0}.{1}", "Properties", p.Alias), result.ErrorMessage); + AddError(actionContext.ModelState, result, p.Alias); } } - if (!p.ValidationRegExp.IsNullOrWhiteSpace()) + if (p.ValidationRegExp.IsNullOrWhiteSpace() == false) { foreach (var result in p.PropertyEditor.ValueEditor.RegexValidator.Validate(postedValue, p.ValidationRegExp, preValues, editor)) { - //add a model state error for the entire property - actionContext.ModelState.AddModelError(string.Format("{0}.{1}", "Properties", p.Alias), result.ErrorMessage); + AddError(actionContext.ModelState, result, p.Alias); } } } return actionContext.ModelState.IsValid; } + + /// + /// Adds the error to model state correctly for a property so we can use it on the client side. + /// + /// + /// + /// + private void AddError(ModelStateDictionary modelState, ValidationResult result, string propertyAlias) + { + //if there are no member names supplied then we assume that the validation message is for the overall property + // not a sub field on the property editor + if (!result.MemberNames.Any()) + { + //add a model state error for the entire property + modelState.AddModelError(string.Format("{0}.{1}", "Properties", propertyAlias), result.ErrorMessage); + } + else + { + //there's assigned field names so we'll combine the field name with the property name + // so that we can try to match it up to a real sub field of this editor + foreach (var field in result.MemberNames) + { + modelState.AddModelError(string.Format("{0}.{1}.{2}", "Properties", propertyAlias, field), result.ErrorMessage); + } + } + } } } \ No newline at end of file