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

Commit

Permalink
feat(timepicker): add validation information
Browse files Browse the repository at this point in the history
- Add validation information for specific violations, allowing users to style classes generated by Angular

Closes #6230
Closes #6259
  • Loading branch information
bifodus authored and wesleycho committed Sep 25, 2016
1 parent f9f7e02 commit 9666c64
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/timepicker/test/timepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,7 @@ describe('timepicker directive', function() {
changeInputValueTo(el, 'pizza');
expect($rootScope.time).toBe(null);
expect(el.parent().hasClass('has-error')).toBe(true);
expect(el.hasClass('ng-invalid-hours'));
expect(element.hasClass('ng-invalid-time')).toBe(true);

changeInputValueTo(el, 8);
Expand All @@ -1247,6 +1248,7 @@ describe('timepicker directive', function() {
changeInputValueTo(el, '8a');
expect($rootScope.time).toBe(null);
expect(el.parent().hasClass('has-error')).toBe(true);
expect(el.hasClass('ng-invalid-minutes'));
expect(element.hasClass('ng-invalid-time')).toBe(true);

changeInputValueTo(el, 22);
Expand All @@ -1262,6 +1264,7 @@ describe('timepicker directive', function() {
changeInputValueTo(el, 'pizza');
expect($rootScope.time).toBe(null);
expect(el.parent().hasClass('has-error')).toBe(true);
expect(el.hasClass('ng-invalid-seconds'));
expect(element.hasClass('ng-invalid-time')).toBe(true);

changeInputValueTo(el, 13);
Expand Down Expand Up @@ -1291,6 +1294,9 @@ describe('timepicker directive', function() {
elS.blur();
$rootScope.$digest();

expect(elH.hasClass('ng-valid'));
expect(elM.hasClass('ng-valid'));
expect(elS.hasClass('ng-valid'));
expect(element.hasClass('ng-invalid-time')).toBe(false);
});

Expand Down
26 changes: 26 additions & 0 deletions src/timepicker/timepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ angular.module('ui.bootstrap.timepicker', [])
})

.controller('UibTimepickerController', ['$scope', '$element', '$attrs', '$parse', '$log', '$locale', 'uibTimepickerConfig', function($scope, $element, $attrs, $parse, $log, $locale, timepickerConfig) {
var hoursModelCtrl, minutesModelCtrl, secondsModelCtrl;
var selected = new Date(),
watchers = [],
ngModelCtrl = { $setViewValue: angular.noop }, // nullModelCtrl
Expand All @@ -36,6 +37,10 @@ angular.module('ui.bootstrap.timepicker', [])
minutesInputEl = inputs.eq(1),
secondsInputEl = inputs.eq(2);

hoursModelCtrl = hoursInputEl.controller('ngModel');
minutesModelCtrl = minutesInputEl.controller('ngModel');
secondsModelCtrl = secondsInputEl.controller('ngModel');

var mousewheel = angular.isDefined($attrs.mousewheel) ? $scope.$parent.$eval($attrs.mousewheel) : timepickerConfig.mousewheel;

if (mousewheel) {
Expand Down Expand Up @@ -295,14 +300,23 @@ angular.module('ui.bootstrap.timepicker', [])
ngModelCtrl.$setValidity('time', false);
if (angular.isDefined(invalidHours)) {
$scope.invalidHours = invalidHours;
if (hoursModelCtrl) {
hoursModelCtrl.$setValidity('hours', false);
}
}

if (angular.isDefined(invalidMinutes)) {
$scope.invalidMinutes = invalidMinutes;
if (minutesModelCtrl) {
minutesModelCtrl.$setValidity('minutes', false);
}
}

if (angular.isDefined(invalidSeconds)) {
$scope.invalidSeconds = invalidSeconds;
if (secondsModelCtrl) {
secondsModelCtrl.$setValidity('seconds', false);
}
}
};

Expand Down Expand Up @@ -425,6 +439,18 @@ angular.module('ui.bootstrap.timepicker', [])
}

function makeValid() {
if (hoursModelCtrl) {
hoursModelCtrl.$setValidity('hours', true);
}

if (minutesModelCtrl) {
minutesModelCtrl.$setValidity('minutes', true);
}

if (secondsModelCtrl) {
secondsModelCtrl.$setValidity('seconds', true);
}

ngModelCtrl.$setValidity('time', true);
$scope.invalidHours = false;
$scope.invalidMinutes = false;
Expand Down

0 comments on commit 9666c64

Please sign in to comment.