Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 525e1a6

Browse files
committed
fix(ngList):Make ngList work with input of type email, url
Validity for inputs are checked for single value using static regexp, which won't work with ngList directive on the input, since it's getting us an array of attributes.
1 parent 08f376f commit 525e1a6

File tree

2 files changed

+127
-29
lines changed

2 files changed

+127
-29
lines changed

src/ng/directive/input.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -519,10 +519,9 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
519519

520520
function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
521521
textInputType(scope, element, attr, ctrl, $sniffer, $browser);
522-
523522
ctrl.$parsers.push(function(value) {
524523
var empty = ctrl.$isEmpty(value);
525-
if (empty || NUMBER_REGEXP.test(value)) {
524+
if (empty || NUMBER_REGEXP.test(value)) {
526525
ctrl.$setValidity('number', true);
527526
return value === '' ? null : (empty ? value : parseFloat(value));
528527
} else {
@@ -581,9 +580,14 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
581580

582581
function urlInputType(scope, element, attr, ctrl, $sniffer, $browser) {
583582
textInputType(scope, element, attr, ctrl, $sniffer, $browser);
584-
583+
var testedValid;
585584
var urlValidator = function(value) {
586-
if (ctrl.$isEmpty(value) || URL_REGEXP.test(value)) {
585+
if (isArray(value)) {
586+
testedValid = value.every(function(val){return URL_REGEXP.test(val)})
587+
} else {
588+
testedValid = URL_REGEXP.test(value)
589+
}
590+
if (ctrl.$isEmpty(value) || testedValid) {
587591
ctrl.$setValidity('url', true);
588592
return value;
589593
} else {
@@ -600,7 +604,13 @@ function emailInputType(scope, element, attr, ctrl, $sniffer, $browser) {
600604
textInputType(scope, element, attr, ctrl, $sniffer, $browser);
601605

602606
var emailValidator = function(value) {
603-
if (ctrl.$isEmpty(value) || EMAIL_REGEXP.test(value)) {
607+
var testedValid;
608+
if (isArray(value)) {
609+
testedValid = value.every(function(val){return EMAIL_REGEXP.test(val)})
610+
} else {
611+
testedValid = EMAIL_REGEXP.test(value)
612+
}
613+
if (ctrl.$isEmpty(value) || testedValid) {
604614
ctrl.$setValidity('email', true);
605615
return value;
606616
} else {

test/ng/directive/inputSpec.js

Lines changed: 112 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -726,50 +726,138 @@ describe('input', function() {
726726
});
727727

728728
describe('email', function() {
729+
describe('should validate e-mail', function(){
730+
it('when used simply', function() {
731+
compileInput('<input type="email" ng-model="email" name="alias" />');
732+
var widget = scope.form.alias;
729733

730-
it('should validate e-mail', function() {
731-
compileInput('<input type="email" ng-model="email" name="alias" />');
734+
changeInputValueTo('[email protected]');
735+
expect(scope.email).toBe('[email protected]');
736+
expect(inputElm).toBeValid();
737+
expect(widget.$error.email).toBe(false);
732738

733-
var widget = scope.form.alias;
734-
changeInputValueTo('[email protected]');
739+
changeInputValueTo('invalid@');
740+
expect(scope.email).toBeUndefined();
741+
expect(inputElm).toBeInvalid();
742+
expect(widget.$error.email).toBeTruthy();
743+
});
744+
it('when used with ngList (default separator)', function() {
745+
compileInput('<input ng-list type="email" ng-model="email" name="alias" />');
746+
var widget = scope.form.alias;
735747

736-
expect(scope.email).toBe('[email protected]');
737-
expect(inputElm).toBeValid();
738-
expect(widget.$error.email).toBe(false);
748+
changeInputValueTo('[email protected]');
749+
expect(scope.email).toEqual(['[email protected]']);
750+
expect(inputElm).toBeValid();
751+
expect(widget.$error.email).toBe(false);
739752

740-
changeInputValueTo('invalid@');
741-
expect(scope.email).toBeUndefined();
742-
expect(inputElm).toBeInvalid();
743-
expect(widget.$error.email).toBeTruthy();
744-
});
753+
changeInputValueTo('[email protected], [email protected]');
754+
expect(scope.email).toEqual(['[email protected]', '[email protected]']);
755+
expect(inputElm).toBeValid();
756+
expect(widget.$error.email).toBe(false);
757+
758+
changeInputValueTo('invalid@');
759+
expect(scope.email).toBeUndefined();
760+
expect(inputElm).toBeInvalid();
761+
expect(widget.$error.email).toBeTruthy();
762+
});
763+
it('when used with ngList (custom separator)', function() {
764+
compileInput('<input ng-list=";" type="email" ng-model="email" name="alias" />');
745765

766+
var widget = scope.form.alias;
767+
changeInputValueTo('[email protected]');
768+
expect(scope.email).toEqual(['[email protected]']);
769+
expect(inputElm).toBeValid();
770+
expect(widget.$error.email).toBe(false);
771+
772+
changeInputValueTo('[email protected];[email protected]');
773+
expect(scope.email).toEqual(['[email protected]', '[email protected]']);
774+
expect(inputElm).toBeValid();
775+
expect(widget.$error.email).toBe(false);
776+
777+
changeInputValueTo('[email protected], [email protected]');
778+
expect(scope.email).toBeUndefined();
779+
expect(inputElm).toBeInvalid();
780+
expect(widget.$error.email).toBeTruthy();
781+
782+
783+
changeInputValueTo('invalid@');
784+
expect(scope.email).toBeUndefined();
785+
expect(inputElm).toBeInvalid();
786+
expect(widget.$error.email).toBeTruthy();
787+
});
788+
});
746789

747-
describe('EMAIL_REGEXP', function() {
748790

791+
describe('EMAIL_REGEXP', function(){
749792
it('should validate email', function() {
750793
expect(EMAIL_REGEXP.test('[email protected]')).toBe(true);
751794
expect(EMAIL_REGEXP.test('[email protected]')).toBe(true);
752795
expect(EMAIL_REGEXP.test('[email protected]')).toBe(false);
753796
});
754797
});
798+
755799
});
756800

757801

758802
describe('url', function() {
803+
describe('should validate url', function(){
804+
it('when used simply', function() {
805+
compileInput('<input type="url" ng-model="url" name="alias" />');
806+
var widget = scope.form.alias;
759807

760-
it('should validate url', function() {
761-
compileInput('<input type="url" ng-model="url" name="alias" />');
762-
var widget = scope.form.alias;
808+
changeInputValueTo('http://www.something.com');
809+
expect(scope.url).toBe('http://www.something.com');
810+
expect(inputElm).toBeValid();
811+
expect(widget.$error.url).toBe(false);
763812

764-
changeInputValueTo('http://www.something.com');
765-
expect(scope.url).toBe('http://www.something.com');
766-
expect(inputElm).toBeValid();
767-
expect(widget.$error.url).toBe(false);
813+
changeInputValueTo('invalid.com');
814+
expect(scope.url).toBeUndefined();
815+
expect(inputElm).toBeInvalid();
816+
expect(widget.$error.url).toBeTruthy();
817+
});
818+
it('when used with ngList (default separator)', function() {
819+
compileInput('<input ng-list type="url" ng-model="url" name="alias" />');
768820

769-
changeInputValueTo('invalid.com');
770-
expect(scope.url).toBeUndefined();
771-
expect(inputElm).toBeInvalid();
772-
expect(widget.$error.url).toBeTruthy();
821+
var widget = scope.form.alias;
822+
changeInputValueTo('http://www.something.com');
823+
expect(scope.url).toEqual(['http://www.something.com']);
824+
expect(inputElm).toBeValid();
825+
expect(widget.$error.url).toBe(false);
826+
827+
changeInputValueTo('http://www.something.com, http://www.somethingelse.com');
828+
expect(scope.url).toEqual(['http://www.something.com', 'http://www.somethingelse.com']);
829+
expect(inputElm).toBeValid();
830+
expect(widget.$error.url).toBe(false);
831+
832+
changeInputValueTo('http:');
833+
expect(scope.url).toBeUndefined();
834+
expect(inputElm).toBeInvalid();
835+
expect(widget.$error.url).toBeTruthy();
836+
});
837+
it('when used with ngList (custom separator)', function() {
838+
compileInput('<input ng-list=";" type="url" ng-model="url" name="alias" />');
839+
840+
var widget = scope.form.alias;
841+
changeInputValueTo('http://www.something.com');
842+
expect(scope.url).toEqual(['http://www.something.com']);
843+
expect(inputElm).toBeValid();
844+
expect(widget.$error.url).toBe(false);
845+
846+
changeInputValueTo('http://www.something.com;http://www.somethingelse.com');
847+
expect(scope.url).toEqual(['http://www.something.com', 'http://www.somethingelse.com']);
848+
expect(inputElm).toBeValid();
849+
expect(widget.$error.url).toBe(false);
850+
851+
changeInputValueTo('http://www.something.com, http://www.somethingelse.com');
852+
expect(scope.url).toBeUndefined();
853+
expect(inputElm).toBeInvalid();
854+
expect(widget.$error.url).toBeTruthy();
855+
856+
changeInputValueTo('http:');
857+
expect(scope.url).toBeUndefined();
858+
expect(inputElm).toBeInvalid();
859+
expect(widget.$error.url).toBeTruthy();
860+
});
773861
});
774862

775863

0 commit comments

Comments
 (0)