Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7e94a7b

Browse filesBrowse files
committed
[Form] Derive "data_class" option from passed "multiple" option in FileType
1 parent 46a8ede commit 7e94a7b
Copy full SHA for 7e94a7b

File tree

2 files changed

+22
-4
lines changed
Filter options

2 files changed

+22
-4
lines changed

‎src/Symfony/Component/Form/Extension/Core/Type/FileType.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/FileType.php
+7-4Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Form\AbstractType;
1515
use Symfony\Component\Form\FormInterface;
1616
use Symfony\Component\Form\FormView;
17+
use Symfony\Component\OptionsResolver\Options;
1718
use Symfony\Component\OptionsResolver\OptionsResolver;
1819

1920
class FileType extends AbstractType
@@ -39,19 +40,21 @@ public function buildView(FormView $view, FormInterface $form, array $options)
3940
*/
4041
public function finishView(FormView $view, FormInterface $form, array $options)
4142
{
42-
$view
43-
->vars['multipart'] = true
44-
;
43+
$view->vars['multipart'] = true;
4544
}
4645

4746
/**
4847
* {@inheritdoc}
4948
*/
5049
public function configureOptions(OptionsResolver $resolver)
5150
{
51+
$dataClass = function (Options $options) {
52+
return $options['multiple'] ? null : 'Symfony\Component\HttpFoundation\File\File';
53+
};
54+
5255
$resolver->setDefaults(array(
5356
'compound' => false,
54-
'data_class' => 'Symfony\Component\HttpFoundation\File\File',
57+
'data_class' => $dataClass,
5558
'empty_data' => null,
5659
'multiple' => false,
5760
));

‎src/Symfony/Component/Form/Tests/Extension/Core/Type/FileTypeTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/Type/FileTypeTest.php
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ public function testSubmitEmpty()
4444
$this->assertNull($form->getData());
4545
}
4646

47+
public function testSetDataMultiple()
48+
{
49+
$form = $this->factory->createBuilder('Symfony\Component\Form\Extension\Core\Type\FileType', null, [
50+
'multiple' => true,
51+
])->getForm();
52+
53+
$data = array(
54+
$this->createUploadedFileMock('abcdef', 'first.jpg', true),
55+
$this->createUploadedFileMock('zyxwvu', 'second.jpg', true),
56+
);
57+
58+
$form->setData($data);
59+
$this->assertSame($data, $form->getData());
60+
}
61+
4762
public function testSubmitMultiple()
4863
{
4964
$form = $this->factory->createBuilder('Symfony\Component\Form\Extension\Core\Type\FileType', null, array(

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.