From 13007647706181ee8bfe14667267c287a6735445 Mon Sep 17 00:00:00 2001 From: Warren Buckley Date: Wed, 4 Jan 2017 14:12:45 +0000 Subject: [PATCH] Adding in server side validation - but need to clarify that this is the 'right' way to do it that wires up correctly/magically back to the UI components --- src/Umbraco.Web/Editors/TemplateController.cs | 13 +++++++++++++ .../Models/ContentEditing/TemplateDisplay.cs | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/src/Umbraco.Web/Editors/TemplateController.cs b/src/Umbraco.Web/Editors/TemplateController.cs index eec76ca34e..d0c17b7f4b 100644 --- a/src/Umbraco.Web/Editors/TemplateController.cs +++ b/src/Umbraco.Web/Editors/TemplateController.cs @@ -6,8 +6,10 @@ using System.Web.Http; using AutoMapper; using Umbraco.Core.IO; using Umbraco.Core.Models; +using Umbraco.Core.Services; using Umbraco.Web.Models.ContentEditing; using Umbraco.Web.Mvc; +using Umbraco.Web.WebApi; using Umbraco.Web.WebApi.Filters; using Constants = Umbraco.Core.Constants; @@ -86,6 +88,17 @@ namespace Umbraco.Web.Editors /// public TemplateDisplay PostSave(TemplateDisplay display) { + + //Checking the submitted is valid with the Required attributes decorated on the ViewModel + if (ModelState.IsValid == false) + { + //Unsure of the standard or set way to do this?! + //throw new HttpResponseException(Request.CreateNotificationValidationErrorResponse("MISSING NAME")); + + throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); + } + + if (display.Id > 0) { // update diff --git a/src/Umbraco.Web/Models/ContentEditing/TemplateDisplay.cs b/src/Umbraco.Web/Models/ContentEditing/TemplateDisplay.cs index dacccb8ceb..3c1ebe16c3 100644 --- a/src/Umbraco.Web/Models/ContentEditing/TemplateDisplay.cs +++ b/src/Umbraco.Web/Models/ContentEditing/TemplateDisplay.cs @@ -1,21 +1,26 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; +using Umbraco.Core.Models.Validation; namespace Umbraco.Web.Models.ContentEditing { [DataContract(Name = "template", Namespace = "")] public class TemplateDisplay { + [DataMember(Name = "id")] public int Id { get; set; } + [Required] [DataMember(Name = "name")] public string Name { get; set; } + [RequiredForPersistence] [DataMember(Name = "alias")] public string Alias { get; set; }