Skip to content
Open
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
18 changes: 10 additions & 8 deletions src/FormHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,11 @@
*/
public function populate(
FormModelInterface $model,
mixed $data,
array $data,
?array $map = null,
?bool $strict = null,
?string $scope = null
): bool {
if (!is_array($data)) {
return false;
}

$scope ??= $model->getFormName();
if ($scope === '') {
$hydrateData = $data;
Expand All @@ -68,13 +64,13 @@
if (!isset($data[$scope]) || !is_array($data[$scope])) {
return false;
}
$hydrateData = $data[$scope];

Check failure on line 67 in src/FormHydrator.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedArrayAccess

src/FormHydrator.php:67:28: MixedArrayAccess: Cannot access array value on mixed variable $data (see https://psalm.dev/051)

Check failure on line 67 in src/FormHydrator.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.4-ubuntu-latest

MixedArrayAccess

src/FormHydrator.php:67:28: MixedArrayAccess: Cannot access array value on mixed variable $data (see https://psalm.dev/051)
}

$this->hydrator->hydrate(
$model,
new ArrayData(
$hydrateData,

Check failure on line 73 in src/FormHydrator.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedArgument

src/FormHydrator.php:73:17: MixedArgument: Argument 1 of Yiisoft\Hydrator\ArrayData::__construct cannot be mixed, expecting array<array-key, mixed> (see https://psalm.dev/030)

Check failure on line 73 in src/FormHydrator.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.4-ubuntu-latest

MixedArgument

src/FormHydrator.php:73:17: MixedArgument: Argument 1 of Yiisoft\Hydrator\ArrayData::__construct cannot be mixed, expecting array<array-key, mixed> (see https://psalm.dev/030)
$this->createMap($model, $map, $strict),
$strict ?? true
)
Expand Down Expand Up @@ -152,7 +148,13 @@
return false;
}

return $this->populate($model, $request->getParsedBody(), $map, $strict, $scope);
return $this->populate(
$model,
array_merge($request->getParsedBody(), $request->getUploadedFiles()),

Check failure on line 153 in src/FormHydrator.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

PossiblyInvalidArgument

src/FormHydrator.php:153:25: PossiblyInvalidArgument: Argument 1 of array_merge expects array<array-key, mixed>, but possibly different type array<array-key, mixed>|null|object provided (see https://psalm.dev/092)

Check failure on line 153 in src/FormHydrator.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.4-ubuntu-latest

PossiblyInvalidArgument

src/FormHydrator.php:153:25: PossiblyInvalidArgument: Argument 1 of array_merge expects array<array-key, mixed>, but possibly different type array<array-key, mixed>|null|object provided (see https://psalm.dev/092)
$map,
$strict,
$scope
);
}

/**
Expand All @@ -179,11 +181,11 @@
?bool $strict = null,
?string $scope = null
): bool {
if ($request->getMethod() !== 'POST') {
if (!$this->populateFromPost($model, $request, $map, $strict, $scope)) {
return false;
}

return $this->populateAndValidate($model, $request->getParsedBody(), $map, $strict, $scope);
return $this->validate($model)->isValid();
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/FormHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
namespace Yiisoft\FormModel\Tests;

use HttpSoft\Message\ServerRequestFactory;
use HttpSoft\Message\StreamFactory;
use HttpSoft\Message\UploadedFile;
use HttpSoft\Message\UploadedFileFactory;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\FormModel\FormModel;
use Yiisoft\FormModel\Tests\Support\Form\CarForm;
use Yiisoft\FormModel\Tests\Support\Form\UploadedFileForm;
use Yiisoft\FormModel\Tests\Support\TestHelper;
use Yiisoft\Validator\Result;
use Yiisoft\Validator\Rule\Integer;
Expand Down Expand Up @@ -180,4 +184,25 @@ public function getRules(): iterable
$this->assertSame('developer', $form->job);
$this->assertSame('', $form->tip);
}

public function testUploadedFile(): void
{
$stream = (new StreamFactory())
->createStreamFromFile('./Support/Dto/Coordinates.php')
;
$uploadedFile = (new UploadedFileFactory())
->createUploadedFile($stream)
;
$request = (new ServerRequestFactory())
->createServerRequest('POST', '/')
->withUploadedFiles([$uploadedFile])
;

$form = new UploadedFileForm();

$result = TestHelper::createFormHydrator()->populateFromPostAndValidate($form, $request);

$this->assertTrue($result);
$this->assertInstanceOf(UploadedFile::class, $form->file);
}
}
23 changes: 23 additions & 0 deletions tests/Support/Form/UploadedFileForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Yiisoft\FormModel\Tests\Support\Form;

use Yiisoft\FormModel\FormModel;
use Yiisoft\Input\Http\Attribute\Parameter\UploadedFiles;
use Yiisoft\Validator\Rule\Required;
use Yiisoft\Validator\RulesProviderInterface;

final class UploadedFileForm extends FormModel implements RulesProviderInterface
{
#[UploadedFiles('UploadedFileForm.file')]
private ?UploadedFile $file = null;

public function getRules(): array
{
return [
'file' => [new Required()],
];
}
}
Loading