Skip to content

[Form] Filter arrays out of scalar form types #29307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,6 @@ public function testThrowExceptionIfDefaultProtocolIsInvalid()
));
}

public function testSubmitWithNonStringDataDoesNotBreakTheFixUrlProtocolListener()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting test 😄

{
$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
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public function testDontValidateConstraintsIfNoValidationGroups()
->getForm();

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

$this->expectNoValidate();

Expand Down