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

Commit

Permalink
fix(ui-date): Validator is ok for blank or null dates
Browse files Browse the repository at this point in the history
Blank and null dates are valid
  • Loading branch information
alexanderchan committed Feb 18, 2016
1 parent 6097c2b commit 93e6141
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export default angular.module('ui.date', [])
dateToString: dateToString,
};

function dateToString(dateFormat, value) {
dateFormat = dateFormat || uiDateFormatConfig;
function dateToString(uiDateFormat, value) {
var dateFormat = uiDateFormat || uiDateFormatConfig;
if (value) {
if (dateFormat) {
try {
Expand Down Expand Up @@ -138,8 +138,10 @@ export default angular.module('ui.date', [])
}
});

controller.$validators.uiDateValidator = function uiDateValidator(modelValue, viewValue) {
return angular.isDate(uiDateConverter.stringToDate(attrs.uiDateFormat, viewValue));
controller.$validators.uiDateValidator = function uiDateValidator(modelValue, viewValue) {
return viewValue === null
|| viewValue === ''
|| angular.isDate(uiDateConverter.stringToDate(attrs.uiDateFormat, viewValue));
};

controller.$parsers.push(function uiDateParser(valueToParse) {
Expand Down
23 changes: 23 additions & 0 deletions src/date.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,29 @@ describe('uiDateFormat', function() {
expect(element.controller('ngModel').$viewValue).toEqual(null);
});
});

it('should validate null and blank dates as valid', function() {
inject(function($compile, $rootScope) {

var element = $compile('<form name="testForm"><input name="optionalDate" ui-date="{dateFormat: \'yy-mm-dd\'}" ng-required="false" ng-model="x"/></form>')($rootScope);
var ngModel = element.find('input').controller('ngModel');
ngModel.$setViewValue('');

expect($rootScope.testForm.$error).toEqual({});
})
});

// it('should validate null and blank dates as valid', function() {
// inject(function($compile, $rootScope) {
// $rootScope.x = null;
// var element = $compile('<form name="testForm"><input name="optionalDate" ui-date="{dateFormat: \'yy-mm-dd\'}" ng-required="false" ng-model="x"/></form>')($rootScope);
// expect($rootScope.testForm.$error).toEqual({});
//
// element.find('input').val('09-99-99').triggerHandler('input');
// expect($rootScope.testForm.$error).toEqual({});
// })
// });

});

describe('$parsing', function() {
Expand Down

0 comments on commit 93e6141

Please sign in to comment.