[Form][DX] FileType "multiple" fixes

This commit is contained in:
Yonel Ceruto 2016-11-02 11:44:02 -04:00 committed by Fabien Potencier
parent 0221ffcb8f
commit 36b7ba64f4
2 changed files with 63 additions and 5 deletions

View File

@ -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,
));
}

View File

@ -44,6 +44,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('file', null, array(