diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index 103263b1..2f342598 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -1346,6 +1346,60 @@ public function seeCurrentActionIs(string $action): void $this->fail("Action '$action' does not exist"); } + /** + * Verifies that multiple fields on a form have errors. + * + * If you only specify the name of the fields, this method will + * verify that the field contains at least one error of any type: + * + * ``` php + * seeFormErrorMessages(['telephone', 'address']); + * ``` + * + * If you want to specify the error messages, you can do so + * by sending an associative array instead, with the key being + * the name of the field and the error message the value. + * + * This method will validate that the expected error message + * is contained in the actual error message, that is, + * you can specify either the entire error message or just a part of it: + * + * ``` php + * seeFormErrorMessages([ + * 'address' => 'The address is too long' + * 'telephone' => 'too short', // the full error message is 'The telephone is too short' + * ]); + * ``` + * + * If you don't want to specify the error message for some fields, + * you can pass `null` as value instead of the message string, + * or you can directly omit the value of that field. If that is the case, + * it will be validated that that field has at least one error of any type: + * + * ``` php + * seeFormErrorMessages([ + * 'telephone' => 'too short', + * 'address' => null, + * 'postal code', + * ]); + * ``` + * + * @param string[] $expectedErrors + */ + public function seeFormErrorMessages(array $expectedErrors): void + { + foreach ($expectedErrors as $field => $message) { + if (is_int($field)) { + $this->seeFormErrorMessage($message); + } else { + $this->seeFormErrorMessage($field, $message); + } + } + } + /** * Verifies that a form field has an error. * You can specify the expected error message as second parameter.