From a439490569ac28dfd8f759fb9b42d3cb56490ee3 Mon Sep 17 00:00:00 2001 From: Florian FERBACH Date: Mon, 3 Apr 2017 16:14:49 +0200 Subject: [PATCH 1/3] Do not update date field actual value if invalid --- src/javascripts/ng-admin/Crud/field/maDateField.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/javascripts/ng-admin/Crud/field/maDateField.js b/src/javascripts/ng-admin/Crud/field/maDateField.js index 2957ab1a..8c7a8871 100644 --- a/src/javascripts/ng-admin/Crud/field/maDateField.js +++ b/src/javascripts/ng-admin/Crud/field/maDateField.js @@ -24,9 +24,12 @@ export default function maDateField() { scope.rawValue = scope.value == null ? null : valueToRawValue(scope.value); scope.$watch('rawValue', function(newRawValue) { + // ui bootstrap datepicker set value to undefined + // when the value is invalid and null if empty + const invalid = newRawValue === undefined; const newValue = field.parse()(newRawValue); - if (!angular.equals(scope.value, newValue)) { + if (!angular.equals(scope.value, newValue) && !invalid) { scope.value = newValue; } }); From cafc44e5e682e95acc0ef86ab1f968bec3efceb5 Mon Sep 17 00:00:00 2001 From: Florian FERBACH Date: Mon, 3 Apr 2017 16:46:29 +0200 Subject: [PATCH 2/3] Apply review --- src/javascripts/ng-admin/Crud/field/maDateField.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/javascripts/ng-admin/Crud/field/maDateField.js b/src/javascripts/ng-admin/Crud/field/maDateField.js index 8c7a8871..1c4b4cdd 100644 --- a/src/javascripts/ng-admin/Crud/field/maDateField.js +++ b/src/javascripts/ng-admin/Crud/field/maDateField.js @@ -24,12 +24,14 @@ export default function maDateField() { scope.rawValue = scope.value == null ? null : valueToRawValue(scope.value); scope.$watch('rawValue', function(newRawValue) { - // ui bootstrap datepicker set value to undefined - // when the value is invalid and null if empty - const invalid = newRawValue === undefined; + if(newRawValue === undefined){ + // ui bootstrap datepicker set value to undefined + // when the value is invalid and null if empty + return; + } const newValue = field.parse()(newRawValue); - if (!angular.equals(scope.value, newValue) && !invalid) { + if (!angular.equals(scope.value, newValue)) { scope.value = newValue; } }); From 718b010838bc60adc058c29f9b727f2da746cc21 Mon Sep 17 00:00:00 2001 From: Florian FERBACH Date: Mon, 3 Apr 2017 16:46:40 +0200 Subject: [PATCH 3/3] Add test --- .../test/unit/Crud/field/maDateFieldSpec.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/javascripts/test/unit/Crud/field/maDateFieldSpec.js b/src/javascripts/test/unit/Crud/field/maDateFieldSpec.js index e1a16455..5607eaa2 100644 --- a/src/javascripts/test/unit/Crud/field/maDateFieldSpec.js +++ b/src/javascripts/test/unit/Crud/field/maDateFieldSpec.js @@ -89,4 +89,21 @@ describe('directive: date-field', function() { expect(isolatedScope.value).toBe('2010-01-01'); }); + + it('should not update value if input is invalid', () => { + const now = '2016-09-18'; + scope.value = now; + scope.field = new DateField(); + + const element = $compile(directiveUsage)(scope); + const isolatedScope = element.isolateScope(); + + element.find('input').val('2016-09-1').triggerHandler('input'); + isolatedScope.$digest(); + expect(isolatedScope.value).toBe('2016-09-18'); + + element.find('input').val('2016-09-19').triggerHandler('input'); + isolatedScope.$digest(); + expect(isolatedScope.value).toBe('2016-09-19'); + }) });