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

Commit 5e80c4f

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 5e80c4f

File tree

3 files changed

+136
-27
lines changed

3 files changed

+136
-27
lines changed

src/Angular.js

+8
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,14 @@ function map(obj, iterator, context) {
516516
return results;
517517
}
518518

519+
function every(array, fun) {
520+
for (var i = 0; i < array.length; i++) {
521+
if (i in array && !fun.call(array, array[i], i, Object(array))) {
522+
return false;
523+
}
524+
}
525+
return true;
526+
}
519527

520528
/**
521529
* @description

src/ng/directive/input.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var URL_REGEXP = /^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\
44
var EMAIL_REGEXP = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$/;
55
var NUMBER_REGEXP = /^\s*(\-|\+)?(\d+|(\d*(\.\d*)))\s*$/;
66

7+
8+
79
var inputType = {
810

911
/**
@@ -581,9 +583,14 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
581583

582584
function urlInputType(scope, element, attr, ctrl, $sniffer, $browser) {
583585
textInputType(scope, element, attr, ctrl, $sniffer, $browser);
584-
586+
var testedValid;
585587
var urlValidator = function(value) {
586-
if (ctrl.$isEmpty(value) || URL_REGEXP.test(value)) {
588+
if (isArray(value)) {
589+
testedValid = every(value, function(val){return URL_REGEXP.test(val)})
590+
} else {
591+
testedValid = URL_REGEXP.test(value)
592+
}
593+
if (ctrl.$isEmpty(value) || testedValid) {
587594
ctrl.$setValidity('url', true);
588595
return value;
589596
} else {
@@ -600,7 +607,13 @@ function emailInputType(scope, element, attr, ctrl, $sniffer, $browser) {
600607
textInputType(scope, element, attr, ctrl, $sniffer, $browser);
601608

602609
var emailValidator = function(value) {
603-
if (ctrl.$isEmpty(value) || EMAIL_REGEXP.test(value)) {
610+
var testedValid;
611+
if (isArray(value)) {
612+
testedValid = every(value, function(val){return EMAIL_REGEXP.test(val)})
613+
} else {
614+
testedValid = EMAIL_REGEXP.test(value)
615+
}
616+
if (ctrl.$isEmpty(value) || testedValid) {
604617
ctrl.$setValidity('email', true);
605618
return value;
606619
} else {

test/ng/directive/inputSpec.js

+112-24
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)