Skip to content

Commit

Permalink
Merge pull request #1334 from marmelab/fix_datepicker_values_disappea…
Browse files Browse the repository at this point in the history
…ring

[RFR] Do not update date field actual value if invalid
  • Loading branch information
jpetitcolas committed Apr 3, 2017
2 parents a3209bb + 718b010 commit 9a8957f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/javascripts/ng-admin/Crud/field/maDateField.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
17 changes: 17 additions & 0 deletions src/javascripts/test/unit/Crud/field/maDateFieldSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
})
});

0 comments on commit 9a8957f

Please sign in to comment.