@@ -1108,6 +1108,65 @@ public function seeCurrentActionIs(string $action)
1108
1108
$ this ->fail ("Action ' $ action' does not exist " );
1109
1109
}
1110
1110
1111
+ /**
1112
+ * Verifies that a form field has an error.
1113
+ * You can specify the expected error message as second parameter.
1114
+ *
1115
+ * ``` php
1116
+ * <?php
1117
+ * $I->seeFormErrorMessage('username');
1118
+ * $I->seeFormErrorMessage('username', 'Username is empty');
1119
+ * ```
1120
+ * @param string $field
1121
+ * @param string|null $message
1122
+ */
1123
+ public function seeFormErrorMessage (string $ field , $ message = null )
1124
+ {
1125
+ $ formCollector = $ this ->grabCollector ('form ' , __FUNCTION__ );
1126
+
1127
+ if (!$ forms = $ formCollector ->getData ()->getValue ('forms ' )['forms ' ]) {
1128
+ $ this ->fail ('There are no forms on the current page. ' );
1129
+ }
1130
+
1131
+ $ fields = [];
1132
+ $ errors = [];
1133
+
1134
+ foreach ($ forms as $ form ) {
1135
+ foreach ($ form ['children ' ] as $ child ) {
1136
+ $ fieldName = $ child ['name ' ];
1137
+ $ fields [] = $ fieldName ;
1138
+
1139
+ if (!array_key_exists ('errors ' , $ child )) {
1140
+ continue ;
1141
+ }
1142
+ foreach ($ child ['errors ' ] as $ error ) {
1143
+ $ errors [$ fieldName ] = $ error ['message ' ];
1144
+ }
1145
+ }
1146
+ }
1147
+
1148
+ if (array_search ($ field , $ fields ) === false ) {
1149
+ $ this ->fail ("the field ' $ field' does not exist in the form. " );
1150
+ }
1151
+
1152
+ if (!array_key_exists ($ field , $ errors )) {
1153
+ $ this ->fail ("No form error message for field ' $ field'. " );
1154
+ }
1155
+
1156
+ if (!$ message ) {
1157
+ return ;
1158
+ }
1159
+
1160
+ $ this ->assertStringContainsString (
1161
+ $ message ,
1162
+ $ errors [$ field ],
1163
+ sprintf (
1164
+ "There is an error message for the field '%s', but it does not match the expected message. " ,
1165
+ $ field
1166
+ )
1167
+ );
1168
+ }
1169
+
1111
1170
/**
1112
1171
* Checks that the user's password would not benefit from rehashing.
1113
1172
* If the user is not provided it is taken from the current session.
0 commit comments