From 2212de3455842ee1cbafafa306f2b811389c6940 Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 16 Jul 2013 18:52:58 +1000 Subject: [PATCH] Got server side field validation wired up to non-user defined properties (such as name). --- .../common/directives/valserver.directive.js | 2 +- .../directives/valserverfield.directive.js | 56 +++++++++++++++++++ .../services/servervalidation.service.js | 4 +- .../views/directives/umb-content-name.html | 14 +++-- src/Umbraco.Web/Editors/ContentController.cs | 2 +- .../Models/ContentEditing/ContentItemBasic.cs | 2 +- 6 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 src/Umbraco.Web.UI.Client/src/common/directives/valserverfield.directive.js diff --git a/src/Umbraco.Web.UI.Client/src/common/directives/valserver.directive.js b/src/Umbraco.Web.UI.Client/src/common/directives/valserver.directive.js index 887f961ac8..4755eca7a3 100644 --- a/src/Umbraco.Web.UI.Client/src/common/directives/valserver.directive.js +++ b/src/Umbraco.Web.UI.Client/src/common/directives/valserver.directive.js @@ -2,7 +2,7 @@ * @ngdoc object * @name umbraco.directive:valServer * @restrict A - * @description This directive is used to associate a field with a server-side validation response + * @description This directive is used to associate a content property with a server-side validation response * so that the validators in angular are updated based on server-side feedback. **/ function valServer(serverValidationService) { diff --git a/src/Umbraco.Web.UI.Client/src/common/directives/valserverfield.directive.js b/src/Umbraco.Web.UI.Client/src/common/directives/valserverfield.directive.js new file mode 100644 index 0000000000..3d1b124fcb --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/common/directives/valserverfield.directive.js @@ -0,0 +1,56 @@ +/** + * @ngdoc object + * @name umbraco.directive:valServerField + * @restrict A + * @description This directive is used to associate a content field (not user defined) with a server-side validation response + * so that the validators in angular are updated based on server-side feedback. + **/ +function valServerField(serverValidationService) { + return { + require: 'ngModel', + restrict: "A", + link: function (scope, element, attr, ctrl) { + + if (!attr.valServerField) { + throw "valServerField must have a field name for referencing server errors"; + } + + var fieldName = attr.valServerField; + + //subscribe to the changed event of the view model. This is required because when we + // have a server error we actually invalidate the form which means it cannot be + // resubmitted. So once a field is changed that has a server error assigned to it + // we need to re-validate it for the server side validator so the user can resubmit + // the form. Of course normal client-side validators will continue to execute. + ctrl.$viewChangeListeners.push(function () { + if (ctrl.$invalid) { + ctrl.$setValidity('valServerField', true); + //emit the event upwards + scope.$emit("serverRevalidated", { modelCtrl: ctrl }); + } + }); + + //subscribe to the server validation changes + serverValidationService.subscribe(null, fieldName, function (isValid, fieldErrors, allErrors) { + if (!isValid) { + ctrl.$setValidity('valServerField', false); + //assign an error msg property to the current validator + ctrl.errorMsg = fieldErrors[0].errorMsg; + } + else { + ctrl.$setValidity('valServerField', true); + //reset the error message + ctrl.errorMsg = ""; + } + }); + + //when the element is disposed we need to unsubscribe! + // NOTE: this is very important otherwise when this controller re-binds the previous subscriptsion will remain + // but they are a different callback instance than the above. + element.bind('$destroy', function () { + serverValidationService.unsubscribe(null, fieldName); + }); + } + }; +} +angular.module('umbraco.directives').directive("valServerField", valServerField); \ No newline at end of file diff --git a/src/Umbraco.Web.UI.Client/src/common/services/servervalidation.service.js b/src/Umbraco.Web.UI.Client/src/common/services/servervalidation.service.js index 4584d8b3d5..8a32f7151b 100644 --- a/src/Umbraco.Web.UI.Client/src/common/services/servervalidation.service.js +++ b/src/Umbraco.Web.UI.Client/src/common/services/servervalidation.service.js @@ -38,7 +38,7 @@ function serverValidationService() { return item.propertyAlias === null && item.fieldName === fieldName; }); if (!exists1) { - callbacks.push({ propertyAlias: contentProperty.alias, fieldName: fieldName, callback: callback }); + callbacks.push({ propertyAlias: null, fieldName: fieldName, callback: callback }); } } else if (contentProperty !== undefined) { @@ -104,7 +104,7 @@ function serverValidationService() { * @description * Gets all callbacks that has been registered using the subscribe method for the field. */ - getFieldCallbacks: function (contentProperty, fieldName) { + getFieldCallbacks: function (fieldName) { var found = _.filter(callbacks, function (item) { //returns any callback that have been registered directly against the field return (item.propertyAlias === null && item.fieldName === fieldName); diff --git a/src/Umbraco.Web.UI.Client/src/views/directives/umb-content-name.html b/src/Umbraco.Web.UI.Client/src/views/directives/umb-content-name.html index 8b412eeb8e..45c1c3093e 100644 --- a/src/Umbraco.Web.UI.Client/src/views/directives/umb-content-name.html +++ b/src/Umbraco.Web.UI.Client/src/views/directives/umb-content-name.html @@ -1,6 +1,10 @@ - -
- +
+ + + Required -
- \ No newline at end of file + {{contentNameForm.name.errorMsg}} + +
diff --git a/src/Umbraco.Web/Editors/ContentController.cs b/src/Umbraco.Web/Editors/ContentController.cs index f92e284c0e..ad2ad26cd7 100644 --- a/src/Umbraco.Web/Editors/ContentController.cs +++ b/src/Umbraco.Web/Editors/ContentController.cs @@ -87,7 +87,7 @@ namespace Umbraco.Web.Editors throw new HttpResponseException(HttpStatusCode.NotFound); } - var emptyContent = new Content("Empty", parentId, contentType); + var emptyContent = new Content("", parentId, contentType); return _contentModelMapper.ToContentItemDisplay(emptyContent); } diff --git a/src/Umbraco.Web/Models/ContentEditing/ContentItemBasic.cs b/src/Umbraco.Web/Models/ContentEditing/ContentItemBasic.cs index 590b1a1cbd..270fecc33c 100644 --- a/src/Umbraco.Web/Models/ContentEditing/ContentItemBasic.cs +++ b/src/Umbraco.Web/Models/ContentEditing/ContentItemBasic.cs @@ -32,7 +32,7 @@ namespace Umbraco.Web.Models.ContentEditing public int Id { get; set; } [DataMember(Name = "name", IsRequired = true)] - [Required(AllowEmptyStrings = false)] + [Required(AllowEmptyStrings = false, ErrorMessage = "Required")] public string Name { get; set; } [DataMember(Name = "properties")]