Skip to content

Commit

Permalink
Adapt after removing result set in validator (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Feb 10, 2022
1 parent a20ed4a commit 6d75cbc
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
16 changes: 7 additions & 9 deletions src/FormModel.php
Expand Up @@ -12,7 +12,7 @@
use Yiisoft\Strings\Inflector;
use Yiisoft\Strings\StringHelper;
use Yiisoft\Validator\PostValidationHookInterface;
use Yiisoft\Validator\ResultSet;
use Yiisoft\Validator\Result;
use Yiisoft\Validator\RulesProviderInterface;

use function array_key_exists;
Expand Down Expand Up @@ -196,18 +196,16 @@ public function setAttribute(string $name, $value): void
}
}

public function processValidationResult(ResultSet $resultSet): void
public function processValidationResult(Result $result): void
{
$this->validated = false;

/** @var array<string, Resultset> $resultSet */
foreach ($resultSet as $attribute => $result) {
if ($result->isValid() === false) {
$this->formErrors->clear($attribute);
/** @psalm-suppress InvalidArgument */
$this->addErrors([$attribute => $result->getErrors()]);
}
foreach ($result->getErrorsIndexedByAttribute() as $attribute => $errors) {
$this->formErrors->clear($attribute);
/** @psalm-suppress InvalidArgument */
$this->addErrors([$attribute => $errors]);
}

$this->validated = true;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/TestSupport/Form/PersonalForm.php
Expand Up @@ -36,8 +36,8 @@ public function getAttributeHints(): array
public function getRules(): array
{
return [
'name' => [Required::rule(), HasLength::rule()->min(4)->tooShortMessage('Is too short.')],
'email' => [Email::rule()],
'name' => [Required::rule(), HasLength::rule()->min(4)->tooShortMessage('Is too short.')],
'password' => [
Required::rule(),
(MatchRegularExpression::rule("/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}/"))
Expand Down
4 changes: 2 additions & 2 deletions tests/TestSupport/Validator/ValidatorMock.php
Expand Up @@ -5,7 +5,7 @@
namespace Yiisoft\Form\Tests\TestSupport\Validator;

use Yiisoft\Validator\Formatter;
use Yiisoft\Validator\ResultSet;
use Yiisoft\Validator\Result;
use Yiisoft\Validator\Validator;
use Yiisoft\Validator\ValidatorInterface;

Expand All @@ -18,7 +18,7 @@ public function __construct()
$this->validator = new Validator(new Formatter());
}

public function validate($dataSet, iterable $rules = []): ResultSet
public function validate($dataSet, iterable $rules = []): Result
{
return $this->validator->validate($dataSet, $rules);
}
Expand Down
16 changes: 8 additions & 8 deletions tests/Widget/ErrorSummaryTest.php
Expand Up @@ -23,8 +23,8 @@ public function dataProviderErrorSummary(): array
return [
// Default settings.
[
'jac',
'jack@.com',
'jac',
'A258*f',
[],
'',
Expand All @@ -37,17 +37,17 @@ public function dataProviderErrorSummary(): array
<div>
<p class="text-danger">Please fix the following errors:</p>
<ul>
<li>Is too short.</li>
<li>This value is not a valid email address.</li>
<li>Is too short.</li>
<li>Must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters.</li>
</ul>
</div>
HTML,
],
// Set custom header and custom footer.
[
'jac',
'jack@.com',
'jac',
'A258*f',
[],
'Custom header',
Expand All @@ -60,8 +60,8 @@ public function dataProviderErrorSummary(): array
<div>
<p class="text-danger">Custom header</p>
<ul>
<li>Is too short.</li>
<li>This value is not a valid email address.</li>
<li>Is too short.</li>
<li>Must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters.</li>
</ul>
<p class="text-primary">Custom footer</p>
Expand All @@ -70,8 +70,8 @@ public function dataProviderErrorSummary(): array
],
// Set only attributes with showAllErros its `true`.
[
'jac',
'jack@.com',
'jac',
'A258*f',
[],
'',
Expand All @@ -91,8 +91,8 @@ public function dataProviderErrorSummary(): array
],
// Set only attributes with showAllErros `false`.
[
'jac',
'jack@.com',
'jac',
'A258*f',
[],
'',
Expand Down Expand Up @@ -147,8 +147,8 @@ public function testImmutability(): void
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException
*/
public function testErrorSummary(
string $name,
string $email,
string $name,
string $password,
array $attributes,
string $header,
Expand All @@ -163,8 +163,8 @@ public function testErrorSummary(

$record = [
'PersonalForm' => [
'name' => $name,
'email' => $email,
'name' => $name,
'password' => $password,
],
];
Expand Down

0 comments on commit 6d75cbc

Please sign in to comment.