From 3d6eea94706bb77738ff0f6b5eb833fe01e0bdcd Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 7 Nov 2013 14:58:41 +1100 Subject: [PATCH] Fixes: U4-3350 Content Validation: Can't get rid of DateTime error --- .../PropertyEditors/PropertyValueEditor.cs | 4 +++- .../directives/validation/valserver.directive.js | 11 +++++++---- .../datepicker/datepicker.controller.js | 14 ++++++++------ .../PropertyEditors/DateTimeValidator.cs | 8 +++++++- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/Umbraco.Core/PropertyEditors/PropertyValueEditor.cs b/src/Umbraco.Core/PropertyEditors/PropertyValueEditor.cs index e1787b1b3b..0ce00288c8 100644 --- a/src/Umbraco.Core/PropertyEditors/PropertyValueEditor.cs +++ b/src/Umbraco.Core/PropertyEditors/PropertyValueEditor.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Xml.Linq; using Newtonsoft.Json; +using Umbraco.Core.Logging; using Umbraco.Core.Manifest; using Umbraco.Core.Models; using Umbraco.Core.Models.Editors; @@ -223,7 +224,8 @@ namespace Umbraco.Core.PropertyEditors var result = TryConvertValueToCrlType(editorValue.Value); if (result.Success == false) { - throw new InvalidOperationException("The value " + editorValue + " cannot be converted to the type " + GetDatabaseType()); + LogHelper.Warn("The value " + editorValue.Value + " cannot be converted to the type " + GetDatabaseType()); + return null; } return result.Result; } diff --git a/src/Umbraco.Web.UI.Client/src/common/directives/validation/valserver.directive.js b/src/Umbraco.Web.UI.Client/src/common/directives/validation/valserver.directive.js index c4df72923d..02d1677c95 100644 --- a/src/Umbraco.Web.UI.Client/src/common/directives/validation/valserver.directive.js +++ b/src/Umbraco.Web.UI.Client/src/common/directives/validation/valserver.directive.js @@ -26,17 +26,20 @@ function valServer(serverValidationManager) { } } - //subscribe to the changed event of the view model. This is required because when we + //Need to watch the value model for it to change, previously we had subscribed to + //ctrl.$viewChangeListeners but this is not good enough if you have an editor that + // doesn't specifically have a 2 way ng binding. 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. - //TODO: Should we be using $render here instead? - ctrl.$viewChangeListeners.push(function () { + scope.$watch(function() { + return ctrl.$modelValue; + }, function (newValue) { if (ctrl.$invalid) { ctrl.$setValidity('valServer', true); } - }); + }); //subscribe to the server validation changes serverValidationManager.subscribe(currentProperty.alias, fieldName, function (isValid, propertyErrors, allErrors) { diff --git a/src/Umbraco.Web.UI.Client/src/views/propertyeditors/datepicker/datepicker.controller.js b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/datepicker/datepicker.controller.js index 2ee3c3bcf7..85f3f2c83c 100644 --- a/src/Umbraco.Web.UI.Client/src/views/propertyeditors/datepicker/datepicker.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/datepicker/datepicker.controller.js @@ -1,5 +1,5 @@ angular.module("umbraco").controller("Umbraco.PropertyEditors.DatepickerController", - function ($scope, notificationsService, assetsService) { + function ($scope, notificationsService, assetsService, angularHelper) { //setup the default config var config = { @@ -12,17 +12,19 @@ angular.module("umbraco").controller("Umbraco.PropertyEditors.DatepickerControll //map back to the model $scope.model.config = config; - function applyDate(e){ + function applyDate(e) { + + angularHelper.safeApply($scope, function() { // when a date is changed, update the model if (e.localDate) { if ($scope.model.config.format == "yyyy-MM-dd HH:mm:ss") { - $scope.$apply(function(){ - $scope.model.value = e.localDate.toIsoDateTimeString(); - }); - }else{ + $scope.model.value = e.localDate.toIsoDateTimeString(); + } + else { $scope.model.value = e.localDate.toIsoDateString(); } } + }); } assetsService.loadJs( diff --git a/src/Umbraco.Web/PropertyEditors/DateTimeValidator.cs b/src/Umbraco.Web/PropertyEditors/DateTimeValidator.cs index b1d61e25d9..8a41f3c399 100644 --- a/src/Umbraco.Web/PropertyEditors/DateTimeValidator.cs +++ b/src/Umbraco.Web/PropertyEditors/DateTimeValidator.cs @@ -14,8 +14,14 @@ namespace Umbraco.Web.PropertyEditors { public IEnumerable Validate(object value, PreValueCollection preValues, PropertyEditor editor) { + //don't validate if empty + if (value == null || value.ToString().IsNullOrWhiteSpace()) + { + yield break; + } + DateTime dt; - if (value != null && DateTime.TryParse(value.ToString(), out dt) == false) + if (DateTime.TryParse(value.ToString(), out dt) == false) { yield return new ValidationResult(string.Format("The string value {0} cannot be parsed into a DateTime", value), new[]