From 507f82afde5b980eee7a2c48c4aef144066bb3c8 Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Wed, 2 Nov 2016 11:44:02 -0400 Subject: [PATCH 1/2] [Form] FileType "multiple" fixes --- .../Form/Extension/Core/Type/FileType.php | 41 ++++++++++++++++--- .../Extension/Core/Type/FileTypeTest.php | 27 ++++++++++++ 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Core/Type/FileType.php b/src/Symfony/Component/Form/Extension/Core/Type/FileType.php index bc24899de5cb0..3609306bae3b6 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/FileType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/FileType.php @@ -12,12 +12,37 @@ namespace Symfony\Component\Form\Extension\Core\Type; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormEvent; +use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormView; +use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; class FileType extends AbstractType { + /** + * {@inheritdoc} + */ + public function buildForm(FormBuilderInterface $builder, array $options) + { + if ($options['multiple']) { + $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) { + $form = $event->getForm(); + $data = $event->getData(); + + // submitted data for an input file (no required) without choosing any file + if (array(null) === $data) { + $emptyData = $form->getConfig()->getEmptyData(); + + $data = is_callable($emptyData) ? call_user_func($emptyData, $form, $data) : $emptyData; + $event->setData($data); + } + }); + } + } + /** * {@inheritdoc} */ @@ -39,9 +64,7 @@ public function buildView(FormView $view, FormInterface $form, array $options) */ public function finishView(FormView $view, FormInterface $form, array $options) { - $view - ->vars['multipart'] = true - ; + $view->vars['multipart'] = true; } /** @@ -49,10 +72,18 @@ public function finishView(FormView $view, FormInterface $form, array $options) */ public function configureOptions(OptionsResolver $resolver) { + $dataClass = function (Options $options) { + return $options['multiple'] ? null : 'Symfony\Component\HttpFoundation\File\File'; + }; + + $emptyData = function (Options $options) { + return $options['multiple'] ? array() : null; + }; + $resolver->setDefaults(array( 'compound' => false, - 'data_class' => 'Symfony\Component\HttpFoundation\File\File', - 'empty_data' => null, + 'data_class' => $dataClass, + 'empty_data' => $emptyData, 'multiple' => false, )); } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/FileTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/FileTypeTest.php index 55555efecb7bf..c6a61af8b7159 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/FileTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/FileTypeTest.php @@ -44,6 +44,33 @@ public function testSubmitEmpty() $this->assertNull($form->getData()); } + public function testSubmitEmptyMultiple() + { + $form = $this->factory->createBuilder('file', null, array( + 'multiple' => true, + ))->getForm(); + + // submitted data when an input file is uploaded without choosing any file + $form->submit(array(null)); + + $this->assertSame(array(), $form->getData()); + } + + public function testSetDataMultiple() + { + $form = $this->factory->createBuilder('file', null, array( + 'multiple' => true, + ))->getForm(); + + $data = array( + $this->createUploadedFileMock('abcdef', 'first.jpg', true), + $this->createUploadedFileMock('zyxwvu', 'second.jpg', true), + ); + + $form->setData($data); + $this->assertSame($data, $form->getData()); + } + public function testSubmitMultiple() { $form = $this->factory->createBuilder('file', null, array( From 59695cfc9479847829aa5ca55fd44628202cf1dd Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Wed, 16 Nov 2016 08:17:57 -0500 Subject: [PATCH 2/2] fixed typo --- src/Symfony/Component/Form/Extension/Core/Type/FileType.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/Extension/Core/Type/FileType.php b/src/Symfony/Component/Form/Extension/Core/Type/FileType.php index 3609306bae3b6..914c9402fc8e4 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/FileType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/FileType.php @@ -32,7 +32,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) $form = $event->getForm(); $data = $event->getData(); - // submitted data for an input file (no required) without choosing any file + // submitted data for an input file (not required) without choosing any file if (array(null) === $data) { $emptyData = $form->getConfig()->getEmptyData();