[Form] Filter arrays out of scalar form types

This commit is contained in:
Nicolas Grekas 2018-11-21 13:01:00 +01:00
parent 9e84e0ff98
commit 000e4aab5e
4 changed files with 21 additions and 12 deletions

View File

@ -532,11 +532,12 @@ class Form implements \IteratorAggregate, FormInterface
$submittedData = null;
} elseif (is_scalar($submittedData)) {
$submittedData = (string) $submittedData;
} elseif ($this->config->getOption('allow_file_upload')) {
// no-op
} elseif ($this->config->getRequestHandler()->isFileUpload($submittedData)) {
} elseif (!$this->config->getOption('allow_file_upload') && $this->config->getRequestHandler()->isFileUpload($submittedData)) {
$submittedData = null;
$this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, file upload given.');
} elseif (\is_array($submittedData) && !$this->config->getCompound() && !$this->config->hasOption('multiple')) {
$submittedData = null;
$this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, array given.');
}
$dispatcher = $this->config->getEventDispatcher();

View File

@ -1036,6 +1036,22 @@ class CompoundFormTest extends AbstractFormTest
$this->assertFalse($submit->isSubmitted());
}
public function testArrayTransformationFailureOnSubmit()
{
$this->form->add($this->getBuilder('foo')->setCompound(false)->getForm());
$this->form->add($this->getBuilder('bar', null, null, array('multiple' => false))->setCompound(false)->getForm());
$this->form->submit(array(
'foo' => array('foo'),
'bar' => array('bar'),
));
$this->assertNull($this->form->get('foo')->getData());
$this->assertSame('Submitted data was expected to be text or number, array given.', $this->form->get('foo')->getTransformationFailure()->getMessage());
$this->assertSame(array('bar'), $this->form->get('bar')->getData());
}
public function testFileUpload()
{
$reqHandler = new HttpFoundationRequestHandler();

View File

@ -83,14 +83,6 @@ class UrlTypeTest extends TextTypeTest
));
}
public function testSubmitWithNonStringDataDoesNotBreakTheFixUrlProtocolListener()
{
$form = $this->factory->create(static::TESTED_TYPE);
$form->submit(array('domain.com', 'www.domain.com'));
$this->assertSame(array('domain.com', 'www.domain.com'), $form->getData());
}
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expectedData = 'http://empty')
{
$form = $this->factory->create(static::TESTED_TYPE, null, array(

View File

@ -220,7 +220,7 @@ class FormValidatorTest extends ConstraintValidatorTestCase
->getForm();
// Launch transformer
$form->submit(array());
$form->submit('foo');
$this->expectNoValidate();