yiisoft / yiisoft/form-model

Form with only file upload fails `populateFromPostAndValidate()`

Open
#81 0 comments 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.