Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observe validator changes #266

Merged
merged 2 commits into from
Jan 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions dist/angular-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,11 +682,18 @@ angular.module('validation.directive', ['validation.provider']);
/**
* Set initial validity to undefined if no boolean value is transmitted
*/
var initialValidity;
if (typeof scope.initialValidity === 'boolean') {
initialValidity = scope.initialValidity;
var initialValidity = void 0;
if (typeof attrs.initialValidity === 'boolean') {
initialValidity = attrs.initialValidity;
}

/**
* Observe validator changes in order to allow dynamically change it
*/
attrs.$observe('validator', function(value) {
validation = value.split(',');
});

/**
* Set up groups object in order to keep track validation of elements
*/
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-validation.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/validator.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,13 @@
initialValidity = attrs.initialValidity;
}

/**
* Observe validator changes in order to allow dynamically change it
*/
attrs.$observe('validator', function(value) {
validation = value.split(',');
});

/**
* Set up groups object in order to keep track validation of elements
*/
Expand Down
42 changes: 42 additions & 0 deletions test/unit/directivesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,46 @@ describe('directives', function() {
expect(messageElem.hasClass('validation-invalid')).toBe(true);
});
});

describe('Observing validator changes', function() {
beforeEach(inject(function($injector) {
$rootScope = $injector.get('$rootScope');
$compile = $injector.get('$compile');
$scope = $rootScope.$new();

$scope.validator = 'required';

element = $compile('<form name="Form"><input type="text" name="inputField" ng-model="inputField" validator="{{validator}}"></form>')($scope);
$scope.$digest();
}));

it('Initial should be pristine and invalid', function() {
expect($scope.Form.$pristine).toBe(true);
expect(element.hasClass('ng-pristine')).toBe(true);
expect($scope.Form.$valid).toBeUndefined(true);
expect($scope.Form.$invalid).toBeUndefined(true);
});

it('After input should be dirty and valid', function() {
$scope.Form.inputField.$setViewValue('Some text');

expect($scope.Form.$dirty).toBe(true);
expect(element.hasClass('ng-dirty')).toBe(true);
expect($scope.Form.$valid).toBe(true);
expect(element.hasClass('ng-valid')).toBe(true);
});

it('When validator changes should be invalid', function() {
$scope.$apply(function() {
$scope.validator = 'required, number';
});

$scope.Form.inputField.$setViewValue('Some text');

expect($scope.Form.$valid).toBe(false);
expect(element.hasClass('ng-valid')).toBe(false);
expect($scope.Form.$invalid).toBe(true);
expect(element.hasClass('ng-invalid')).toBe(true);
});
});
});