@@ -390,6 +390,12 @@ var inputType = {
390
390
'reset' : noop
391
391
} ;
392
392
393
+ // A helper function to call $setValidity and return the value / undefined,
394
+ // a pattern that is repeated a lot in the input validation logic.
395
+ function validate ( ctrl , validatorName , validity , value ) {
396
+ ctrl . $setValidity ( validatorName , validity ) ;
397
+ return validity ? value : undefined ;
398
+ }
393
399
394
400
function textInputType ( scope , element , attr , ctrl , $sniffer , $browser ) {
395
401
// In composition mode, users are still inputing intermediate text buffer,
@@ -474,22 +480,15 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
474
480
patternValidator ,
475
481
match ;
476
482
477
- var validate = function ( regexp , value ) {
478
- if ( ctrl . $isEmpty ( value ) || regexp . test ( value ) ) {
479
- ctrl . $setValidity ( 'pattern' , true ) ;
480
- return value ;
481
- } else {
482
- ctrl . $setValidity ( 'pattern' , false ) ;
483
- return undefined ;
484
- }
485
- } ;
486
-
487
483
if ( pattern ) {
484
+ var validateRegex = function ( regexp , value ) {
485
+ return validate ( ctrl , 'pattern' , ctrl . $isEmpty ( value ) || regexp . test ( value ) , value ) ;
486
+ } ;
488
487
match = pattern . match ( / ^ \/ ( .* ) \/ ( [ g i m ] * ) $ / ) ;
489
488
if ( match ) {
490
489
pattern = new RegExp ( match [ 1 ] , match [ 2 ] ) ;
491
490
patternValidator = function ( value ) {
492
- return validate ( pattern , value ) ;
491
+ return validateRegex ( pattern , value ) ;
493
492
} ;
494
493
} else {
495
494
patternValidator = function ( value ) {
@@ -500,7 +499,7 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
500
499
'Expected {0} to be a RegExp but was {1}. Element: {2}' , pattern ,
501
500
patternObj , startingTag ( element ) ) ;
502
501
}
503
- return validate ( patternObj , value ) ;
502
+ return validateRegex ( patternObj , value ) ;
504
503
} ;
505
504
}
506
505
@@ -512,13 +511,7 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
512
511
if ( attr . ngMinlength ) {
513
512
var minlength = int ( attr . ngMinlength ) ;
514
513
var minLengthValidator = function ( value ) {
515
- if ( ! ctrl . $isEmpty ( value ) && value . length < minlength ) {
516
- ctrl . $setValidity ( 'minlength' , false ) ;
517
- return undefined ;
518
- } else {
519
- ctrl . $setValidity ( 'minlength' , true ) ;
520
- return value ;
521
- }
514
+ return validate ( ctrl , 'minlength' , ctrl . $isEmpty ( value ) || value . length >= minlength , value ) ;
522
515
} ;
523
516
524
517
ctrl . $parsers . push ( minLengthValidator ) ;
@@ -529,13 +522,7 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
529
522
if ( attr . ngMaxlength ) {
530
523
var maxlength = int ( attr . ngMaxlength ) ;
531
524
var maxLengthValidator = function ( value ) {
532
- if ( ! ctrl . $isEmpty ( value ) && value . length > maxlength ) {
533
- ctrl . $setValidity ( 'maxlength' , false ) ;
534
- return undefined ;
535
- } else {
536
- ctrl . $setValidity ( 'maxlength' , true ) ;
537
- return value ;
538
- }
525
+ return validate ( ctrl , 'maxlength' , ctrl . $isEmpty ( value ) || value . length <= maxlength , value ) ;
539
526
} ;
540
527
541
528
ctrl . $parsers . push ( maxLengthValidator ) ;
@@ -564,13 +551,7 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
564
551
if ( attr . min ) {
565
552
var minValidator = function ( value ) {
566
553
var min = parseFloat ( attr . min ) ;
567
- if ( ! ctrl . $isEmpty ( value ) && value < min ) {
568
- ctrl . $setValidity ( 'min' , false ) ;
569
- return undefined ;
570
- } else {
571
- ctrl . $setValidity ( 'min' , true ) ;
572
- return value ;
573
- }
554
+ return validate ( ctrl , 'min' , ctrl . $isEmpty ( value ) || value >= min , value ) ;
574
555
} ;
575
556
576
557
ctrl . $parsers . push ( minValidator ) ;
@@ -580,42 +561,23 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
580
561
if ( attr . max ) {
581
562
var maxValidator = function ( value ) {
582
563
var max = parseFloat ( attr . max ) ;
583
- if ( ! ctrl . $isEmpty ( value ) && value > max ) {
584
- ctrl . $setValidity ( 'max' , false ) ;
585
- return undefined ;
586
- } else {
587
- ctrl . $setValidity ( 'max' , true ) ;
588
- return value ;
589
- }
564
+ return validate ( ctrl , 'max' , ctrl . $isEmpty ( value ) || value <= max , value ) ;
590
565
} ;
591
566
592
567
ctrl . $parsers . push ( maxValidator ) ;
593
568
ctrl . $formatters . push ( maxValidator ) ;
594
569
}
595
570
596
571
ctrl . $formatters . push ( function ( value ) {
597
-
598
- if ( ctrl . $isEmpty ( value ) || isNumber ( value ) ) {
599
- ctrl . $setValidity ( 'number' , true ) ;
600
- return value ;
601
- } else {
602
- ctrl . $setValidity ( 'number' , false ) ;
603
- return undefined ;
604
- }
572
+ return validate ( ctrl , 'number' , ctrl . $isEmpty ( value ) || isNumber ( value ) , value ) ;
605
573
} ) ;
606
574
}
607
575
608
576
function urlInputType ( scope , element , attr , ctrl , $sniffer , $browser ) {
609
577
textInputType ( scope , element , attr , ctrl , $sniffer , $browser ) ;
610
578
611
579
var urlValidator = function ( value ) {
612
- if ( ctrl . $isEmpty ( value ) || URL_REGEXP . test ( value ) ) {
613
- ctrl . $setValidity ( 'url' , true ) ;
614
- return value ;
615
- } else {
616
- ctrl . $setValidity ( 'url' , false ) ;
617
- return undefined ;
618
- }
580
+ return validate ( ctrl , 'url' , ctrl . $isEmpty ( value ) || URL_REGEXP . test ( value ) , value ) ;
619
581
} ;
620
582
621
583
ctrl . $formatters . push ( urlValidator ) ;
@@ -626,13 +588,7 @@ function emailInputType(scope, element, attr, ctrl, $sniffer, $browser) {
626
588
textInputType ( scope , element , attr , ctrl , $sniffer , $browser ) ;
627
589
628
590
var emailValidator = function ( value ) {
629
- if ( ctrl . $isEmpty ( value ) || EMAIL_REGEXP . test ( value ) ) {
630
- ctrl . $setValidity ( 'email' , true ) ;
631
- return value ;
632
- } else {
633
- ctrl . $setValidity ( 'email' , false ) ;
634
- return undefined ;
635
- }
591
+ return validate ( ctrl , 'email' , ctrl . $isEmpty ( value ) || EMAIL_REGEXP . test ( value ) , value ) ;
636
592
} ;
637
593
638
594
ctrl . $formatters . push ( emailValidator ) ;
0 commit comments