@@ -1140,10 +1140,8 @@ describe('ngModel', function() {
1140
1140
1141
1141
dealoc ( element ) ;
1142
1142
} ) ) ;
1143
-
1144
1143
} ) ;
1145
1144
1146
-
1147
1145
describe ( 'input' , function ( ) {
1148
1146
var formElm , inputElm , scope , $compile , $sniffer , $browser , changeInputValueTo , currentSpec ;
1149
1147
@@ -2209,7 +2207,6 @@ describe('input', function() {
2209
2207
compileInput ( '<input type="text" name="input" ng-model="value" minlength="3" />' ) ;
2210
2208
expect ( scope . value ) . toBe ( '12345' ) ;
2211
2209
} ) ;
2212
-
2213
2210
} ) ;
2214
2211
2215
2212
@@ -3387,7 +3384,6 @@ describe('input', function() {
3387
3384
expect ( inputElm ) . toBeInvalid ( ) ;
3388
3385
} ) ;
3389
3386
3390
-
3391
3387
it ( 'should render as blank if null' , function ( ) {
3392
3388
compileInput ( '<input type="number" ng-model="age" />' ) ;
3393
3389
@@ -3408,7 +3404,6 @@ describe('input', function() {
3408
3404
expect ( inputElm . val ( ) ) . toBe ( '' ) ;
3409
3405
} ) ;
3410
3406
3411
-
3412
3407
it ( 'should parse empty string to null' , function ( ) {
3413
3408
compileInput ( '<input type="number" ng-model="age" />' ) ;
3414
3409
@@ -3419,7 +3414,6 @@ describe('input', function() {
3419
3414
expect ( inputElm ) . toBeValid ( ) ;
3420
3415
} ) ;
3421
3416
3422
-
3423
3417
it ( 'should only invalidate the model if suffering from bad input when the data is parsed' , function ( ) {
3424
3418
compileInput ( '<input type="number" ng-model="age" />' , {
3425
3419
valid : false ,
@@ -3435,7 +3429,6 @@ describe('input', function() {
3435
3429
expect ( inputElm ) . toBeInvalid ( ) ;
3436
3430
} ) ;
3437
3431
3438
-
3439
3432
it ( 'should validate number if transition from bad input to empty string' , function ( ) {
3440
3433
var validity = {
3441
3434
valid : false ,
@@ -3450,14 +3443,25 @@ describe('input', function() {
3450
3443
expect ( inputElm ) . toBeValid ( ) ;
3451
3444
} ) ;
3452
3445
3446
+ it ( 'should be valid when no value has been set even if it is validated' , function ( ) {
3447
+ // This situation isn't common, but it does arise, when other directives
3448
+ // on the same element observe an attribute (e.g. ngRequired, min, max etc).
3449
+
3450
+ compileInput ( '<input type="number" name="alias" ng-model="value" />' ) ;
3451
+
3452
+ scope . form . alias . $validate ( ) ;
3453
+
3454
+ expect ( inputElm ) . toBeValid ( ) ;
3455
+ expect ( scope . form . alias . $error . number ) . toBeFalsy ( ) ;
3456
+ } ) ;
3457
+
3453
3458
it ( 'should throw if the model value is not a number' , function ( ) {
3454
3459
expect ( function ( ) {
3455
3460
scope . value = 'one' ;
3456
3461
compileInput ( '<input type="number" ng-model="value" />' ) ;
3457
3462
} ) . toThrowMinErr ( 'ngModel' , 'numfmt' , "Expected `one` to be a number" ) ;
3458
3463
} ) ;
3459
3464
3460
-
3461
3465
describe ( 'min' , function ( ) {
3462
3466
3463
3467
it ( 'should validate' , function ( ) {
@@ -3540,7 +3544,6 @@ describe('input', function() {
3540
3544
} ) ;
3541
3545
} ) ;
3542
3546
3543
-
3544
3547
describe ( 'max' , function ( ) {
3545
3548
3546
3549
it ( 'should validate' , function ( ) {
@@ -3623,7 +3626,6 @@ describe('input', function() {
3623
3626
} ) ;
3624
3627
} ) ;
3625
3628
3626
-
3627
3629
describe ( 'required' , function ( ) {
3628
3630
3629
3631
it ( 'should be valid even if value is 0' , function ( ) {
@@ -3655,6 +3657,120 @@ describe('input', function() {
3655
3657
} ) ;
3656
3658
} ) ;
3657
3659
3660
+ describe ( 'ngRequired' , function ( ) {
3661
+
3662
+ describe ( 'when the ngRequired expression initially evaluates to true' , function ( ) {
3663
+
3664
+ it ( 'should be valid even if value is 0' , function ( ) {
3665
+ compileInput ( '<input type="number" ng-model="value" name="alias" ng-required="true" />' ) ;
3666
+
3667
+ changeInputValueTo ( '0' ) ;
3668
+ expect ( inputElm ) . toBeValid ( ) ;
3669
+ expect ( scope . value ) . toBe ( 0 ) ;
3670
+ expect ( scope . form . alias . $error . required ) . toBeFalsy ( ) ;
3671
+ } ) ;
3672
+
3673
+ it ( 'should be valid even if value 0 is set from model' , function ( ) {
3674
+ compileInput ( '<input type="number" ng-model="value" name="alias" ng-required="true" />' ) ;
3675
+
3676
+ scope . $apply ( 'value = 0' ) ;
3677
+
3678
+ expect ( inputElm ) . toBeValid ( ) ;
3679
+ expect ( inputElm . val ( ) ) . toBe ( '0' ) ;
3680
+ expect ( scope . form . alias . $error . required ) . toBeFalsy ( ) ;
3681
+ } ) ;
3682
+
3683
+ it ( 'should register required on non boolean elements' , function ( ) {
3684
+ compileInput ( '<div ng-model="value" name="alias" ng-required="true">' ) ;
3685
+
3686
+ scope . $apply ( "value = ''" ) ;
3687
+
3688
+ expect ( inputElm ) . toBeInvalid ( ) ;
3689
+ expect ( scope . form . alias . $error . required ) . toBeTruthy ( ) ;
3690
+ } ) ;
3691
+
3692
+ it ( 'should be invalid when the user enters an invalid value' , function ( ) {
3693
+ compileInput ( '<input type="number" ng-model="value" name="alias" ng-required="true" />' ) ;
3694
+
3695
+ changeInputValueTo ( 'Hello, world !' ) ;
3696
+ expect ( inputElm . val ( ) ) . toBe ( '' ) ;
3697
+ expect ( inputElm ) . toBeInvalid ( ) ;
3698
+ expect ( scope . form . alias . $error . required ) . toBeTruthy ( ) ;
3699
+ } ) ;
3700
+
3701
+ it ( 'should change from invalid to valid when the value is empty and the ngRequired expression changes to false' , function ( ) {
3702
+ compileInput ( '<input type="number" ng-model="value" name="alias" ng-required="ngRequiredExpr" />' ) ;
3703
+
3704
+ scope . $apply ( 'ngRequiredExpr = true' ) ;
3705
+
3706
+ expect ( inputElm ) . toBeInvalid ( ) ;
3707
+ expect ( scope . value ) . toBeUndefined ( ) ;
3708
+ expect ( scope . form . alias . $error . required ) . toBeTruthy ( ) ;
3709
+
3710
+ scope . $apply ( 'ngRequiredExpr = false' ) ;
3711
+
3712
+ expect ( inputElm ) . toBeValid ( ) ;
3713
+ expect ( scope . value ) . toBeNull ( ) ;
3714
+ expect ( scope . form . alias . $error . required ) . toBeFalsy ( ) ;
3715
+ } ) ;
3716
+ } ) ;
3717
+
3718
+ describe ( 'when the ngRequired expression initially evaluates to false' , function ( ) {
3719
+
3720
+ it ( 'should be valid even if value is empty' , function ( ) {
3721
+ compileInput ( '<input type="number" ng-model="value" name="alias" ng-required="false" />' ) ;
3722
+
3723
+ expect ( inputElm ) . toBeValid ( ) ;
3724
+ expect ( scope . value ) . toBeNull ( ) ;
3725
+ expect ( scope . form . alias . $error . required ) . toBeFalsy ( ) ;
3726
+ expect ( scope . form . alias . $error . number ) . toBeFalsy ( ) ;
3727
+ } ) ;
3728
+
3729
+ it ( 'should be valid if value is non-empty' , function ( ) {
3730
+ compileInput ( '<input type="number" ng-model="value" name="alias" ng-required="false" />' ) ;
3731
+
3732
+ changeInputValueTo ( '42' ) ;
3733
+ expect ( inputElm ) . toBeValid ( ) ;
3734
+ expect ( scope . value ) . toBe ( 42 ) ;
3735
+ expect ( scope . form . alias . $error . required ) . toBeFalsy ( ) ;
3736
+ } ) ;
3737
+
3738
+ it ( 'should not have the "required" error flag set even if the value is not a number' , function ( ) {
3739
+ compileInput ( '<input type="number" ng-model="value" name="alias" ng-required="false" />' ) ;
3740
+
3741
+ changeInputValueTo ( 'Hello, world !' ) ;
3742
+ expect ( inputElm . val ( ) ) . toBe ( '' ) ;
3743
+ expect ( inputElm ) . toBeValid ( ) ;
3744
+ expect ( scope . form . alias . $error . required ) . toBeFalsy ( ) ;
3745
+ } ) ;
3746
+
3747
+ it ( 'should not register required on non boolean elements' , function ( ) {
3748
+ compileInput ( '<div ng-model="value" name="alias" ng-required="false">' ) ;
3749
+
3750
+ scope . $apply ( "value = ''" ) ;
3751
+
3752
+ expect ( inputElm ) . toBeValid ( ) ;
3753
+ expect ( scope . form . alias . $error . required ) . toBeFalsy ( ) ;
3754
+ } ) ;
3755
+
3756
+ it ( 'should change from valid to invalid when the value is empty and the ngRequired expression changes to true' , function ( ) {
3757
+ compileInput ( '<input type="number" ng-model="value" name="alias" ng-required="ngRequiredExpr" />' ) ;
3758
+
3759
+ scope . $apply ( 'ngRequiredExpr = false' ) ;
3760
+
3761
+ expect ( inputElm ) . toBeValid ( ) ;
3762
+ expect ( scope . value ) . toBeNull ( ) ;
3763
+ expect ( scope . form . alias . $error . required ) . toBeFalsy ( ) ;
3764
+
3765
+ scope . $apply ( 'ngRequiredExpr = true' ) ;
3766
+
3767
+ expect ( inputElm ) . toBeInvalid ( ) ;
3768
+ expect ( scope . value ) . toBeUndefined ( ) ;
3769
+ expect ( scope . form . alias . $error . required ) . toBeTruthy ( ) ;
3770
+ } ) ;
3771
+ } ) ;
3772
+ } ) ;
3773
+
3658
3774
describe ( 'minlength' , function ( ) {
3659
3775
3660
3776
it ( 'should invalidate values that are shorter than the given minlength' , function ( ) {
@@ -3700,7 +3816,6 @@ describe('input', function() {
3700
3816
} ) ;
3701
3817
} ) ;
3702
3818
3703
-
3704
3819
describe ( 'maxlength' , function ( ) {
3705
3820
3706
3821
it ( 'should invalidate values that are longer than the given maxlength' , function ( ) {
0 commit comments