Skip to content

[DomCrawler] Don't return file infos for empty file fields #59621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
11 changes: 9 additions & 2 deletions src/Symfony/Component/DomCrawler/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,16 @@ public function getFiles(): array
continue;
}

if ($field instanceof Field\FileFormField) {
$files[$name] = $field->getValue();
if (!$field instanceof Field\FileFormField) {
continue;
}

$value = $field->getValue();
if (empty($value['tmp_name'])) {
continue;
}

$files[$name] = $value;
}

return $files;
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/DomCrawler/Tests/Fixtures/upload.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some content
15 changes: 11 additions & 4 deletions src/Symfony/Component/DomCrawler/Tests/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,16 +472,23 @@ public function testGetFiles()
$this->assertEquals([], $form->getFiles(), '->getFiles() returns an empty array if method is get');

$form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
$this->assertEquals(['foo[bar]' => ['name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0]], $form->getFiles(), '->getFiles() only returns file fields for POST');
$this->assertEquals([], $form->getFiles(), '->getFiles() does not return empty fields');

$form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
$form->get('foo[bar]')->setValue(__DIR__.'/Fixtures/upload.txt');
$this->assertEquals(['foo[bar]'], array_keys($form->getFiles()), '->getFiles() only returns file fields for POST');

$form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>', 'put');
$this->assertEquals(['foo[bar]' => ['name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0]], $form->getFiles(), '->getFiles() only returns file fields for PUT');
$form->get('foo[bar]')->setValue(__DIR__.'/Fixtures/upload.txt');
$this->assertEquals(['foo[bar]'], array_keys($form->getFiles()), '->getFiles() only returns file fields for PUT');

$form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>', 'delete');
$this->assertEquals(['foo[bar]' => ['name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0]], $form->getFiles(), '->getFiles() only returns file fields for DELETE');
$form->get('foo[bar]')->setValue(__DIR__.'/Fixtures/upload.txt');
$this->assertEquals(['foo[bar]'], array_keys($form->getFiles()), '->getFiles() only returns file fields for DELETE');

$form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>', 'patch');
$this->assertEquals(['foo[bar]' => ['name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0]], $form->getFiles(), '->getFiles() only returns file fields for PATCH');
$form->get('foo[bar]')->setValue(__DIR__.'/Fixtures/upload.txt');
$this->assertEquals(['foo[bar]'], array_keys($form->getFiles()), '->getFiles() only returns file fields for PATCH');

$form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" disabled="disabled" /><input type="submit" /></form>');
$this->assertEquals([], $form->getFiles(), '->getFiles() does not include disabled file fields');
Expand Down
Loading