Skip to content

Commit 7a0d734

Browse files
committed
[Form] Derive "data_class" option from passed "multiple" option in FileType
1 parent 0221ffc commit 7a0d734

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

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

Lines 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

Lines 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, array(
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('file', null, array(

0 commit comments

Comments
 (0)