From c0b368862eb43d1b0b5d53d5962f936020b1641a Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Mon, 4 Dec 2023 16:42:35 +0300 Subject: [PATCH] Add yet another test for `Nested` rule --- tests/FormModelTest.php | 31 +++++++++++++++ tests/Support/Form/NestedMixedForm/Body.php | 10 +++++ .../Form/NestedMixedForm/NestedMixedForm.php | 38 +++++++++++++++++++ .../Support/Form/NestedMixedForm/Shipping.php | 10 +++++ 4 files changed, 89 insertions(+) create mode 100644 tests/Support/Form/NestedMixedForm/Body.php create mode 100644 tests/Support/Form/NestedMixedForm/NestedMixedForm.php create mode 100644 tests/Support/Form/NestedMixedForm/Shipping.php diff --git a/tests/FormModelTest.php b/tests/FormModelTest.php index 0dac5b8..0b2a174 100644 --- a/tests/FormModelTest.php +++ b/tests/FormModelTest.php @@ -18,6 +18,7 @@ use Yiisoft\FormModel\Tests\Support\Form\FormWithNestedStructures; use Yiisoft\FormModel\Tests\Support\Form\LoginForm; use Yiisoft\FormModel\Tests\Support\Form\NestedForm; +use Yiisoft\FormModel\Tests\Support\Form\NestedMixedForm\NestedMixedForm; use Yiisoft\FormModel\Tests\Support\Form\NestedRuleForm\MainForm; use Yiisoft\FormModel\Tests\Support\StubInputField; use Yiisoft\FormModel\Tests\Support\TestHelper; @@ -442,4 +443,34 @@ public function testNestedRuleWithFormModels(): void $result->getErrorMessagesIndexedByPath() ); } + + /** + * @see https://github.com/yiisoft/form-model/issues/6 + */ + public function testNestedRuleInForm(): void + { + $form = new NestedMixedForm(); + + TestHelper::createFormHydrator()->populate( + $form, + [ + 'body' => [ + 'shipping' => [ + 'phone' => '+790012345678' + ], + ], + ], + scope: '' + ); + + $result = $form->getValidationResult(); + + $this->assertFalse($result->isValid()); + $this->assertSame( + [ + 'body.shipping.phone' => ['Invalid phone.'], + ], + $result->getErrorMessagesIndexedByPath() + ); + } } diff --git a/tests/Support/Form/NestedMixedForm/Body.php b/tests/Support/Form/NestedMixedForm/Body.php new file mode 100644 index 0000000..2917631 --- /dev/null +++ b/tests/Support/Form/NestedMixedForm/Body.php @@ -0,0 +1,10 @@ +body = new Body(); + } + + public function getRules(): iterable + { + return [ + 'body' => new Nested([ + 'shipping' => [ + new Required(), + new Nested( + [ + 'phone' => new Regex('/^\+\d{11}$/', message: 'Invalid phone.'), + ], + skipOnEmpty: true + ), + ], + ]), + ]; + } +} diff --git a/tests/Support/Form/NestedMixedForm/Shipping.php b/tests/Support/Form/NestedMixedForm/Shipping.php new file mode 100644 index 0000000..a1316b7 --- /dev/null +++ b/tests/Support/Form/NestedMixedForm/Shipping.php @@ -0,0 +1,10 @@ +