diff --git a/src/javascripts/ng-admin/Crud/field/maDateField.js b/src/javascripts/ng-admin/Crud/field/maDateField.js index 2957ab1a..1c4b4cdd 100644 --- a/src/javascripts/ng-admin/Crud/field/maDateField.js +++ b/src/javascripts/ng-admin/Crud/field/maDateField.js @@ -24,6 +24,11 @@ export default function maDateField() { scope.rawValue = scope.value == null ? null : valueToRawValue(scope.value); scope.$watch('rawValue', function(newRawValue) { + 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)) { 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'); + }) });