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 test with nested rule #9

Merged
merged 2 commits 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
28 changes: 28 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\NestedRuleForm\MainForm;
use Yiisoft\FormModel\Tests\Support\StubInputField;
use Yiisoft\FormModel\Tests\Support\TestHelper;
use Yiisoft\Test\Support\Container\SimpleContainer;
Expand Down Expand Up @@ -414,4 +415,31 @@ public function testFormWithNestedStructures(): void
$this->assertSame('12.24', $coordinates->getLatitude());
$this->assertSame('56.78', $coordinates->getLongitude());
}

/**
* @see https://github.com/yiisoft/form-model/issues/7
*/
public function testNestedRuleWithFormModels(): void
{
$form = new MainForm();

TestHelper::createFormHydrator()->populate(
$form,
[
'value' => 'main-form',
'firstLevelForm.secondLevelForm.float' => '-7.1',
],
scope: ''
);

$result = $form->getValidationResult();

$this->assertFalse($result->isValid());
$this->assertSame(
[
'firstLevelForm.secondLevelForm.float' => ['Value must be no less than 0.']
],
$result->getErrorMessagesIndexedByPath()
);
}
}
29 changes: 29 additions & 0 deletions tests/Support/Form/NestedRuleForm/FirstLevelForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

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

use Yiisoft\FormModel\FormModel;
use Yiisoft\Validator\Rule\Integer;
use Yiisoft\Validator\Rule\Nested;
use Yiisoft\Validator\RulesProviderInterface;

final class FirstLevelForm extends FormModel implements RulesProviderInterface
{
private int $number = 1;
private SecondLevelForm $secondLevelForm;

public function __construct()
{
$this->secondLevelForm = new SecondLevelForm();
}

public function getRules(): array
{
return [
'number' => new Integer(min: 1),
'secondLevelForm' => new Nested(),
];
}
}
29 changes: 29 additions & 0 deletions tests/Support/Form/NestedRuleForm/MainForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

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

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

final class MainForm extends FormModel implements RulesProviderInterface
{
public string $value = '';
public FirstLevelForm $firstLevelForm;

public function __construct()
{
$this->firstLevelForm = new FirstLevelForm();
}

public function getRules(): array
{
return [
'value' => new Required(),
'firstLevelForm' => new Nested(),
];
}
}
21 changes: 21 additions & 0 deletions tests/Support/Form/NestedRuleForm/SecondLevelForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

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

use Yiisoft\FormModel\FormModel;
use Yiisoft\Validator\Rule\Number;
use Yiisoft\Validator\RulesProviderInterface;

final class SecondLevelForm extends FormModel implements RulesProviderInterface
{
private float $float = 0.01;

public function getRules(): array
{
return [
'float' => new Number(min: 0),
];
}
}
Loading