Skip to content

Commit

Permalink
Use hydrator (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Oct 8, 2023
1 parent bebd636 commit 52f2024
Show file tree
Hide file tree
Showing 24 changed files with 364 additions and 932 deletions.
5 changes: 4 additions & 1 deletion composer.json
Expand Up @@ -24,6 +24,8 @@
"yiisoft/friendly-exception": "^1.0",
"yiisoft/html": "^3.0",
"yiisoft/http": "^1.2",
"yiisoft/hydrator": "dev-master",
"yiisoft/hydrator-validator": "dev-master",
"yiisoft/strings": "^2.0",
"yiisoft/validator": "^1.0",
"yiisoft/widget": "^2.0"
Expand Down Expand Up @@ -56,7 +58,8 @@
},
"config-plugin": {
"params": "params.php",
"bootstrap": "bootstrap.php"
"bootstrap": "bootstrap.php",
"di": "di.php"
}
},
"config": {
Expand Down
15 changes: 15 additions & 0 deletions config/di.php
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

use Yiisoft\Definitions\Reference;
use Yiisoft\Form\FormHydrator;
use Yiisoft\Hydrator\Validator\ValidatingHydrator;

return [
FormHydrator::class => [
'__construct()' => [
'hydrator' => Reference::to(ValidatingHydrator::class),
],
],
];
3 changes: 3 additions & 0 deletions psalm.xml
Expand Up @@ -13,4 +13,7 @@
<directory name="vendor"/>
</ignoreFiles>
</projectFiles>
<issueHandlers>
<MixedAssignment errorLevel="suppress" />
</issueHandlers>
</psalm>
4 changes: 2 additions & 2 deletions src/Field/Base/FormAttributeTrait.php
Expand Up @@ -92,7 +92,7 @@ final protected function getFirstError(): ?string
{
return $this
->getFormModel()
->getFormErrors()
->getFirstError($this->getFormAttributeName());
->getValidationResult()
?->getAttributeErrorMessages($this->getFormAttributeName())[0] ?? null;
}
}
7 changes: 3 additions & 4 deletions src/Field/Base/ValidationClass/ValidationClassTrait.php
Expand Up @@ -92,13 +92,12 @@ private function addClassesToAttributes(
?string $invalidClass,
?string $validClass,
): void {
if (!$formModel->isValidated()) {
$validationResult = $formModel->getValidationResult();
if ($validationResult === null) {
return;
}

$hasErrors = $formModel
->getFormErrors()
->hasErrors($attributeName);
$hasErrors = !$validationResult->isAttributeValid($attributeName);

if ($hasErrors && $invalidClass !== null) {
Html::addCssClass($attributes, $invalidClass);
Expand Down
107 changes: 0 additions & 107 deletions src/FormErrors.php

This file was deleted.

114 changes: 0 additions & 114 deletions src/FormErrorsInterface.php

This file was deleted.

50 changes: 50 additions & 0 deletions src/FormHydrator.php
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Form;

use Yiisoft\Hydrator\ArrayData;
use Yiisoft\Hydrator\HydratorInterface;

use function is_array;

/**
* @psalm-import-type MapType from ArrayData
*/
final class FormHydrator
{
public function __construct(
private HydratorInterface $hydrator,
) {
}

/**
* @psalm-param MapType $map
*/
public function populate(
FormModel $model,
mixed $data,
array $map = [],
bool $strict = false,
?string $scope = null
): bool {
if (!is_array($data)) {
return false;
}

$scope ??= $model->getFormName();
if ($scope === '') {
$hydrateData = $data;
} else {
if (!isset($data[$scope]) || !is_array($data[$scope])) {
return false;
}
$hydrateData = $data[$scope];
}

$this->hydrator->hydrate($model, new ArrayData($hydrateData, $map, $strict));

return true;
}
}

0 comments on commit 52f2024

Please sign in to comment.