Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add yet another test for Nested rule #10

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions tests/FormModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
);
}
}
10 changes: 10 additions & 0 deletions tests/Support/Form/NestedMixedForm/Body.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Yiisoft\FormModel\Tests\Support\Form\NestedMixedForm;

final class Body
{
private ?Shipping $shipping = null;
}
38 changes: 38 additions & 0 deletions tests/Support/Form/NestedMixedForm/NestedMixedForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Yiisoft\FormModel\Tests\Support\Form\NestedMixedForm;

use Yiisoft\FormModel\FormModel;
use Yiisoft\Validator\Rule\Nested;
use Yiisoft\Validator\Rule\Regex;
use Yiisoft\Validator\Rule\Required;
use Yiisoft\Validator\RulesProviderInterface;

final class NestedMixedForm extends FormModel implements RulesProviderInterface
{
private Body $body;

public function __construct()
{
$this->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
),
],
]),
];
}
}
10 changes: 10 additions & 0 deletions tests/Support/Form/NestedMixedForm/Shipping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Yiisoft\FormModel\Tests\Support\Form\NestedMixedForm;

final class Shipping
{
private string $phone = '';
}
Loading