|
12 | 12 | namespace Symfony\Component\Form\Extension\Core\Type;
|
13 | 13 |
|
14 | 14 | use Symfony\Component\Form\AbstractType;
|
| 15 | +use Symfony\Component\Form\Extension\Core\DataTransformer\FilesToArrayTransformer; |
| 16 | +use Symfony\Component\Form\FormBuilderInterface; |
| 17 | +use Symfony\Component\Form\FormEvent; |
| 18 | +use Symfony\Component\Form\FormEvents; |
15 | 19 | use Symfony\Component\Form\FormInterface;
|
16 | 20 | use Symfony\Component\Form\FormView;
|
17 | 21 | use Symfony\Component\OptionsResolver\Options;
|
18 | 22 | use Symfony\Component\OptionsResolver\OptionsResolver;
|
19 | 23 |
|
20 | 24 | class FileType extends AbstractType
|
21 | 25 | {
|
| 26 | + /** |
| 27 | + * {@inheritdoc} |
| 28 | + */ |
| 29 | + public function buildForm(FormBuilderInterface $builder, array $options) |
| 30 | + { |
| 31 | + if ($options['multiple']) { |
| 32 | + $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) { |
| 33 | + if (array(null) === $event->getData()) { |
| 34 | + $event->setData($event->getForm()->getConfig()->getEmptyData()); |
| 35 | + } |
| 36 | + }); |
| 37 | + } |
| 38 | + } |
| 39 | + |
22 | 40 | /**
|
23 | 41 | * {@inheritdoc}
|
24 | 42 | */
|
@@ -52,10 +70,14 @@ public function configureOptions(OptionsResolver $resolver)
|
52 | 70 | return $options['multiple'] ? null : 'Symfony\Component\HttpFoundation\File\File';
|
53 | 71 | };
|
54 | 72 |
|
| 73 | + $emptyData = function (Options $options) { |
| 74 | + return $options['multiple'] ? array() : null; |
| 75 | + }; |
| 76 | + |
55 | 77 | $resolver->setDefaults(array(
|
56 | 78 | 'compound' => false,
|
57 | 79 | 'data_class' => $dataClass,
|
58 |
| - 'empty_data' => null, |
| 80 | + 'empty_data' => $emptyData, |
59 | 81 | 'multiple' => false,
|
60 | 82 | ));
|
61 | 83 | }
|
|
0 commit comments