Form with only file upload fails `populateFromPostAndValidate()`
Open
Nobody has claimed this yet.
type:bug
- Dominant language
- PHP
- Stars
- 17
- Forks
- 7
- Avg merge
- 58m
- Merged PRs (30d)
- 1
Description
Controller
public function function(Form $form, Model $model, FormHydrator $formHydrator, ServerRequestInterface $request): ResponseInterface
{
$uploadedFiles = $request->getUploadedFiles();
if (isset($uploadedFiles['Form']['sourceImage'])) {
$form->sourceImage = $uploadedFiles['Form']['sourceImage'];
}
if ($formHydrator->populateFromPostAndValidate($form, $request)) {
$model->dostuff($form); // never reached
}
return $this->viewRenderer->render('page', ['form' => $form, 'model' => $model]);
}
Form
<?php
declare(strict_types=1);
namespace App\Form;
use Psr\Http\Message\UploadedFileInterface;
use Yiisoft\Validator\LabelsProviderInterface;
use Yiisoft\Validator\Rule\Image\Image;
use Yiisoft\Validator\Rule\Required;
use Yiisoft\Validator\RulesProviderInterface;
class Form extends \Yiisoft\FormModel\FormModel implements LabelsProviderInterface, RulesProviderInterface
{
public ?UploadedFileInterface $sourceImage = null;
public function getPropertyLabels(): array
{
return [
'sourceImage' => _('Source Image'),
];
}
public function getValidationPropertyLabels(): array
{
return $this->getPropertyLabels();
}
public function getRules(): array
{
return [
'sourceImage' => [new Required(), new Image(minWidth: 80, skipOnEmpty: true)],
];
}
}
With only 1 (or more?) field, that field being a file upload, populateFromPostAndValidate() never returns true. I suspect this is caused by $request->getParsedBody() being empty under such circumstances.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at FormHydrator::populateFromPostAndValidate() and inspect how it handles an empty $request->getParsedBody() when uploaded files are present. Reproduce the controller example with the Form model and its sourceImage rules, then verify that validation succeeds for a form containing only the file upload.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100