Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
fix(datepicker): fix validation with ngRequired
Browse files Browse the repository at this point in the history
- Fix how validation is handled using `ngRequired`

Closes #4002
Fixes #3862
  • Loading branch information
nivivive authored and wesleycho committed Jul 29, 2015
1 parent 2e9177e commit fe0d954
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
6 changes: 5 additions & 1 deletion src/datepicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
} else {
$log.error('Datepicker directive: "ng-model" value must be a Date object, a number of milliseconds since 01.01.1970 or a string representing an RFC2822 or ISO 8601 date.');
}
ngModelCtrl.$setValidity('date', isValid);
}
this.refreshView();
};
Expand Down Expand Up @@ -616,6 +615,11 @@ function ($compile, $parse, $document, $position, dateFilter, dateParser, datepi

function validator(modelValue, viewValue) {
var value = modelValue || viewValue;

if (!attrs.ngRequired && !value) {
return true;
}

if (angular.isNumber(value)) {
value = new Date(value);
}
Expand Down
58 changes: 35 additions & 23 deletions src/datepicker/test/datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,10 @@ describe('datepicker directive', function () {
expect(angular.isDate($rootScope.date)).toBe(true);
});

it('to a date that is invalid, it gets invalid', function() {
it('to a date that is invalid, it doesn\`t update', function() {
$rootScope.date = new Date('pizza');
$rootScope.$digest();
expect(element.hasClass('ng-invalid')).toBeTruthy();
expect(element.hasClass('ng-invalid-date')).toBeTruthy();
expect(getTitle()).toBe('September 2010');
expect(angular.isDate($rootScope.date)).toBe(true);
expect(isNaN($rootScope.date)).toBe(true);
});
Expand All @@ -360,11 +359,10 @@ describe('datepicker directive', function () {
expect(angular.isString($rootScope.date)).toBe(true);
});

it('to a string that cannot be parsed by Date, it gets invalid', function() {
it('to a string that cannot be parsed by Date, it doesn\'t update', function() {
$rootScope.date = 'pizza';
$rootScope.$digest();
expect(element.hasClass('ng-invalid')).toBeTruthy();
expect(element.hasClass('ng-invalid-date')).toBeTruthy();
expect(getTitle()).toBe('September 2010');
expect($rootScope.date).toBe('pizza');
});
});
Expand Down Expand Up @@ -1876,27 +1874,41 @@ describe('datepicker directive', function () {
});

describe('use with `ng-required` directive', function() {
beforeEach(inject(function() {
$rootScope.date = '';
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup ng-required="true"><div>')($rootScope);
$rootScope.$digest();
assignElements(wrapElement);
}));
describe('`ng-required is true`', function() {
beforeEach(inject(function() {
$rootScope.date = '';
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup ng-required="true"><div>')($rootScope);
$rootScope.$digest();
assignElements(wrapElement);
}));

it('should be invalid initially', function() {
expect(inputEl.hasClass('ng-invalid')).toBeTruthy();
});
it('should be invalid initially and when no date', function() {
expect(inputEl.hasClass('ng-invalid')).toBeTruthy();
});

it('should be valid if model has been specified', function() {
$rootScope.date = new Date();
$rootScope.$digest();
expect(inputEl.hasClass('ng-valid')).toBeTruthy();
it('should be valid if model has been specified', function() {
$rootScope.date = new Date();
$rootScope.$digest();
expect(inputEl.hasClass('ng-valid')).toBeTruthy();
});

it('should be valid if model value is a valid timestamp', function() {
$rootScope.date = Date.now();
$rootScope.$digest();
expect(inputEl.hasClass('ng-valid')).toBeTruthy();
});
});
describe('`ng-required is false`', function() {
beforeEach(inject(function() {
$rootScope.date = '';
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup ng-required="false"><div>')($rootScope);
$rootScope.$digest();
assignElements(wrapElement);
}));

it('should be valid if model value is a valid timestamp', function() {
$rootScope.date = Date.now();
$rootScope.$digest();
expect(inputEl.hasClass('ng-valid')).toBeTruthy();
it('should be valid initially and when no date', function() {
expect(inputEl.hasClass('ng-valid')).toBeTruthy();
});
});
});

Expand Down

0 comments on commit fe0d954

Please sign in to comment.