Skip to content

Commit

Permalink
bug #29307 [Form] Filter arrays out of scalar form types (nicolas-gre…
Browse files Browse the repository at this point in the history
…kas)

This PR was merged into the 3.4 branch.

Discussion
----------

[Form] Filter arrays out of scalar form types

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #4102
| License       | MIT
| Doc PR        | -

Replaces fix #20935

Commits
-------

000e4aa [Form] Filter arrays out of scalar form types
  • Loading branch information
nicolas-grekas committed Dec 8, 2018
2 parents 7a46c98 + 000e4aa commit aca0b84
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/Symfony/Component/Form/Form.php
Expand Up @@ -532,11 +532,12 @@ public function submit($submittedData, $clearMissing = true)
$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();
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/Form/Tests/CompoundFormTest.php
Expand Up @@ -1036,6 +1036,22 @@ public function testDisabledButtonIsNotSubmitted()
$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();
Expand Down
Expand Up @@ -83,14 +83,6 @@ public function testThrowExceptionIfDefaultProtocolIsInvalid()
));
}

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(
Expand Down
Expand Up @@ -220,7 +220,7 @@ public function testDontValidateConstraintsIfNoValidationGroups()
->getForm();

// Launch transformer
$form->submit(array());
$form->submit('foo');

$this->expectNoValidate();

Expand Down

0 comments on commit aca0b84

Please sign in to comment.