File: Form-DX-FileType-multiple-fixes.patch

package info (click to toggle)
symfony 2.8.7%2Bdfsg-1.3%2Bdeb9u3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 39,888 kB
  • sloc: php: 225,095; xml: 4,083; sh: 475; ansic: 263; makefile: 127
file content (122 lines) | stat: -rw-r--r-- 4,509 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
From: Yonel Ceruto <yonelceruto@gmail.com>
Date: Wed, 2 Nov 2016 11:44:02 -0400
Subject: [Form][DX] FileType "multiple" fixes

Origin: upstream, https://github.com/symfony/symfony/commit/36b7ba64f4a27c742be010f855d0fb27bfccfb76
---
 .../Form/Extension/Core/Type/FileType.php          | 41 +++++++++++++++++++---
 .../Tests/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 6c67b8d..40da512 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 @@ class FileType extends AbstractType
      */
     public function finishView(FormView $view, FormInterface $form, array $options)
     {
-        $view
-            ->vars['multipart'] = true
-        ;
+        $view->vars['multipart'] = true;
     }
 
     /**
@@ -49,10 +72,18 @@ class FileType extends AbstractType
      */
     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 eebb6ad..cbeeb46 100644
--- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/FileTypeTest.php
+++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/FileTypeTest.php
@@ -54,6 +54,33 @@ class FileTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
         $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('Symfony\Component\Form\Extension\Core\Type\FileType', null, array(