@@ -941,6 +941,183 @@ describe('form', function() {
941
941
expect ( scope . form . $submitted ) . toBe ( false ) ;
942
942
} ) ;
943
943
} ) ;
944
+
945
+ describe ( 'ngFormTopLevel attribute' , function ( ) {
946
+ it ( 'should allow define a form as top level form' , function ( ) {
947
+ doc = jqLite (
948
+ '<ng:form name="parent">' +
949
+ '<ng:form name="child" ng-form-top-level="true">' +
950
+ '<input ng:model="modelA" name="inputA">' +
951
+ '<input ng:model="modelB" name="inputB">' +
952
+ '</ng:form>' +
953
+ '</ng:form>' ) ;
954
+ $compile ( doc ) ( scope ) ;
955
+
956
+ var parent = scope . parent ,
957
+ child = scope . child ,
958
+ inputA = child . inputA ,
959
+ inputB = child . inputB ;
960
+
961
+ inputA . $setValidity ( 'MyError' , false ) ;
962
+ inputB . $setValidity ( 'MyError' , false ) ;
963
+ expect ( parent . $error . MyError ) . toBeFalsy ( ) ;
964
+ expect ( child . $error . MyError ) . toEqual ( [ inputA , inputB ] ) ;
965
+
966
+ inputA . $setValidity ( 'MyError' , true ) ;
967
+ expect ( parent . $error . MyError ) . toBeFalsy ( ) ;
968
+ expect ( child . $error . MyError ) . toEqual ( [ inputB ] ) ;
969
+
970
+ inputB . $setValidity ( 'MyError' , true ) ;
971
+ expect ( parent . $error . MyError ) . toBeFalsy ( ) ;
972
+ expect ( child . $error . MyError ) . toBeFalsy ( ) ;
973
+
974
+ child . $setDirty ( ) ;
975
+ expect ( parent . $dirty ) . toBeFalsy ( ) ;
976
+
977
+ child . $setSubmitted ( ) ;
978
+ expect ( parent . $submitted ) . toBeFalsy ( ) ;
979
+ } ) ;
980
+
981
+
982
+
983
+ it ( 'should stop enter triggered submit from propagating to parent forms' , function ( ) {
984
+ var form = $compile (
985
+ '<form name="parent">' +
986
+ '<ng-form name="topLevelForm" ng-form-top-level="true">' +
987
+ '<input type="text" name="i"/>' +
988
+ '</ng-form>' +
989
+ '</form>' ) ( scope ) ;
990
+ scope . $digest ( ) ;
991
+
992
+ var inputElm = form . find ( 'input' ) . eq ( 0 ) ;
993
+ var topLevelFormElm = form . find ( 'ng-form' ) . eq ( 0 ) ;
994
+
995
+ var parentFormKeypress = jasmine . createSpy ( 'parentFormKeypress' ) ;
996
+ var topLevelFormKeyPress = jasmine . createSpy ( 'topLevelFormKeyPress' ) ;
997
+
998
+ form . on ( 'keypress' , parentFormKeypress ) ;
999
+ topLevelFormElm . on ( 'keypress' , topLevelFormKeyPress ) ;
1000
+
1001
+ browserTrigger ( inputElm [ 0 ] , 'keypress' , { bubbles : true , keyCode :13 } ) ;
1002
+
1003
+ expect ( parentFormKeypress ) . not . toHaveBeenCalled ( ) ;
1004
+ expect ( topLevelFormKeyPress ) . toHaveBeenCalled ( ) ;
1005
+
1006
+ dealoc ( form ) ;
1007
+ } ) ;
1008
+
1009
+
1010
+ it ( 'should chain nested forms as default behaviour' , function ( ) {
1011
+ doc = jqLite (
1012
+ '<ng:form name="parent">' +
1013
+ '<ng:form name="child" >' +
1014
+ '<input ng:model="modelA" name="inputA">' +
1015
+ '<input ng:model="modelB" name="inputB">' +
1016
+ '</ng:form>' +
1017
+ '</ng:form>' ) ;
1018
+ $compile ( doc ) ( scope ) ;
1019
+
1020
+ var parent = scope . parent ,
1021
+ child = scope . child ,
1022
+ inputA = child . inputA ,
1023
+ inputB = child . inputB ;
1024
+
1025
+ inputA . $setValidity ( 'MyError' , false ) ;
1026
+ inputB . $setValidity ( 'MyError' , false ) ;
1027
+ expect ( parent . $error . MyError ) . toEqual ( [ child ] ) ;
1028
+ expect ( child . $error . MyError ) . toEqual ( [ inputA , inputB ] ) ;
1029
+
1030
+ inputA . $setValidity ( 'MyError' , true ) ;
1031
+ expect ( parent . $error . MyError ) . toEqual ( [ child ] ) ;
1032
+ expect ( child . $error . MyError ) . toEqual ( [ inputB ] ) ;
1033
+
1034
+ inputB . $setValidity ( 'MyError' , true ) ;
1035
+ expect ( parent . $error . MyError ) . toBeFalsy ( ) ;
1036
+ expect ( child . $error . MyError ) . toBeFalsy ( ) ;
1037
+
1038
+ child . $setDirty ( ) ;
1039
+ expect ( parent . $dirty ) . toBeTruthy ( ) ;
1040
+
1041
+ child . $setSubmitted ( ) ;
1042
+ expect ( parent . $submitted ) . toBeTruthy ( ) ;
1043
+ } ) ;
1044
+
1045
+ it ( 'should chain nested forms when "ng-form-top-level" is false' , function ( ) {
1046
+ doc = jqLite (
1047
+ '<ng:form name="parent">' +
1048
+ '<ng:form name="child" ng-form-top-level="false">' +
1049
+ '<input ng:model="modelA" name="inputA">' +
1050
+ '<input ng:model="modelB" name="inputB">' +
1051
+ '</ng:form>' +
1052
+ '</ng:form>' ) ;
1053
+ $compile ( doc ) ( scope ) ;
1054
+
1055
+ var parent = scope . parent ,
1056
+ child = scope . child ,
1057
+ inputA = child . inputA ,
1058
+ inputB = child . inputB ;
1059
+
1060
+ inputA . $setValidity ( 'MyError' , false ) ;
1061
+ inputB . $setValidity ( 'MyError' , false ) ;
1062
+ expect ( parent . $error . MyError ) . toEqual ( [ child ] ) ;
1063
+ expect ( child . $error . MyError ) . toEqual ( [ inputA , inputB ] ) ;
1064
+
1065
+ inputA . $setValidity ( 'MyError' , true ) ;
1066
+ expect ( parent . $error . MyError ) . toEqual ( [ child ] ) ;
1067
+ expect ( child . $error . MyError ) . toEqual ( [ inputB ] ) ;
1068
+
1069
+ inputB . $setValidity ( 'MyError' , true ) ;
1070
+ expect ( parent . $error . MyError ) . toBeFalsy ( ) ;
1071
+ expect ( child . $error . MyError ) . toBeFalsy ( ) ;
1072
+
1073
+ child . $setDirty ( ) ;
1074
+ expect ( parent . $dirty ) . toBeTruthy ( ) ;
1075
+
1076
+ child . $setSubmitted ( ) ;
1077
+ expect ( parent . $submitted ) . toBeTruthy ( ) ;
1078
+ } ) ;
1079
+
1080
+ it ( 'should maintain the default behavior for children of a root form' , function ( ) {
1081
+ doc = jqLite (
1082
+ '<ng:form name="parent">' +
1083
+ '<ng:form name="child" ng-form-top-level="true">' +
1084
+ '<ng:form name="grandchild">' +
1085
+ '<input ng:model="modelA" name="inputA">' +
1086
+ '<input ng:model="modelB" name="inputB">' +
1087
+ '</ng:form>' +
1088
+ '</ng:form>' +
1089
+ '</ng:form>' ) ;
1090
+ $compile ( doc ) ( scope ) ;
1091
+
1092
+ var parent = scope . parent ,
1093
+ child = scope . child ,
1094
+ grandchild = scope . grandchild ,
1095
+ inputA = grandchild . inputA ,
1096
+ inputB = grandchild . inputB ;
1097
+
1098
+ inputA . $setValidity ( 'MyError' , false ) ;
1099
+ inputB . $setValidity ( 'MyError' , false ) ;
1100
+ expect ( parent . $error . MyError ) . toBeFalsy ( ) ;
1101
+ expect ( child . $error . MyError ) . toEqual ( [ grandchild ] ) ;
1102
+ expect ( grandchild . $error . MyError ) . toEqual ( [ inputA , inputB ] ) ;
1103
+
1104
+ inputA . $setValidity ( 'MyError' , true ) ;
1105
+ expect ( parent . $error . MyError ) . toBeFalsy ( ) ;
1106
+ expect ( child . $error . MyError ) . toEqual ( [ grandchild ] ) ;
1107
+ expect ( grandchild . $error . MyError ) . toEqual ( [ inputB ] ) ;
1108
+
1109
+ inputB . $setValidity ( 'MyError' , true ) ;
1110
+ expect ( parent . $error . MyError ) . toBeFalsy ( ) ;
1111
+ expect ( child . $error . MyError ) . toBeFalsy ( ) ;
1112
+ expect ( grandchild . $error . MyError ) . toBeFalsy ( ) ;
1113
+
1114
+ child . $setDirty ( ) ;
1115
+ expect ( parent . $dirty ) . toBeFalsy ( ) ;
1116
+
1117
+ child . $setSubmitted ( ) ;
1118
+ expect ( parent . $submitted ) . toBeFalsy ( ) ;
1119
+ } ) ;
1120
+ } ) ;
944
1121
} ) ;
945
1122
946
1123
describe ( 'form animations' , function ( ) {
@@ -1018,4 +1195,5 @@ describe('form animations', function() {
1018
1195
assertValidAnimation ( $animate . queue [ 2 ] , 'addClass' , 'ng-valid-custom-error' ) ;
1019
1196
assertValidAnimation ( $animate . queue [ 3 ] , 'removeClass' , 'ng-invalid-custom-error' ) ;
1020
1197
} ) ) ;
1198
+
1021
1199
} ) ;
0 commit comments