From 40e75946ada37f6674c81e46b9099a601e2dd80e Mon Sep 17 00:00:00 2001 From: Caitlin Potter Date: Sun, 22 Jun 2014 15:37:41 -0400 Subject: [PATCH] fix(input): improve html5 validation support This CL improves mocking support for HTML5 validation, and ensures that it works correctly along with debounced commission of view values. Closes #7936 Closes #7937 --- src/.jshintrc | 1 + src/Angular.js | 5 +++ src/ng/directive/input.js | 57 ++++++++++++++++++++++------------ test/.jshintrc | 1 + test/ng/directive/inputSpec.js | 40 ++++++++++++++++++++++-- 5 files changed, 82 insertions(+), 22 deletions(-) diff --git a/src/.jshintrc b/src/.jshintrc index 0a9094787c39..589805d63ede 100644 --- a/src/.jshintrc +++ b/src/.jshintrc @@ -101,6 +101,7 @@ "assertNotHasOwnProperty": false, "getter": false, "getBlockElements": false, + "VALIDITY_STATE_PROPERTY": false, /* filters.js */ "getFirstThursdayOfYear": false, diff --git a/src/Angular.js b/src/Angular.js index 711483a26fd3..5aa4da1949e0 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -14,6 +14,7 @@ -nodeName_, -uid, -REGEX_STRING_REGEXP, + -VALIDITY_STATE_PROPERTY, -lowercase, -uppercase, @@ -105,6 +106,10 @@ var REGEX_STRING_REGEXP = /^\/(.+)\/([a-z]*)$/; +// The name of a form control's ValidityState property. +// This is used so that it's possible for internal tests to create mock ValidityStates. +var VALIDITY_STATE_PROPERTY = 'validity'; + /** * @ngdoc function * @name angular.lowercase diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index 8f1a42505e0f..177919f116e3 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -862,15 +862,29 @@ function validate(ctrl, validatorName, validity, value){ return validity ? value : undefined; } +function testFlags(validity, flags) { + var i, flag; + if (flags) { + for (i=0; i'); formElm.append(inputElm); $compile(formElm)(scope); @@ -619,6 +626,8 @@ describe('input', function() { } var attrs; + beforeEach(function() { currentSpec = this; }); + afterEach(function() { currentSpec = null; }); beforeEach(module(function($compileProvider) { $compileProvider.directive('attrCapture', function() { return function(scope, element, $attrs) { @@ -2238,6 +2247,33 @@ describe('input', function() { }); + it('should invalidate number if suffering from bad input', function() { + compileInput('', { + valid: false, + badInput: true + }); + + changeInputValueTo('10a'); + expect(scope.age).toBeUndefined(); + expect(inputElm).toBeInvalid(); + }); + + + it('should validate number if transition from bad input to empty string', function() { + var validity = { + valid: false, + badInput: true + }; + compileInput('', validity); + changeInputValueTo('10a'); + validity.badInput = false; + validity.valid = true; + changeInputValueTo(''); + expect(scope.age).toBeNull(); + expect(inputElm).toBeValid(); + }); + + describe('min', function() { it('should validate', function() {