diff --git a/src/Symfony/Component/Form/Extension/Core/Type/FileType.php b/src/Symfony/Component/Form/Extension/Core/Type/FileType.php index bc24899de5cb0..914c9402fc8e4 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 (not 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( pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy