8000 [Form] Filter arrays out of scalar form types · symfony/symfony@000e4aa · GitHub
[go: up one dir, main page]

Skip to content

Commit 000e4aa

Browse files
[Form] Filter arrays out of scalar form types
1 parent 9e84e0f commit 000e4aa

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

src/Symfony/Component/Form/Form.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,11 +532,12 @@ public function submit($submittedData, $clearMissing = true)
532532
$submittedData = null;
533533
} elseif (is_scalar($submittedData)) {
534534
$submittedData = (string) $submittedData;
535-
} elseif ($this->config->getOption('allow_file_upload')) {
536-
// no-op
537-
} elseif ($this->config->getRequestHandler()->isFileUpload($submittedData)) {
535+
} elseif (!$this->config->getOption('allow_file_upload') && $this->config->getRequestHandler()->isFileUpload($submittedData)) {
538536
$submittedData = null;
539537
$this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, file upload given.');
538+
} elseif (\is_array($submittedData) && !$this->config->getCompound() && !$this->config->hasOption('multiple')) {
539+
$submittedData = null;
540+
$this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, array given.');
540541
}
541542

542543
$dispatcher = $this->config->getEventDispatcher();

src/Symfony/Component/Form/Tests/CompoundFormTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,22 @@ public function testDisabledButtonIsNotSubmitted()
10361036
$this->assertFalse($submit->isSubmitted());
10371037
}
10381038

1039+
public function testArrayTransformationFailureOnSubmit()
1040+
{
1041+
$this->form->add($this->getBuilder('foo')->setCompound(false)->getForm());
1042+
$this->form->add($this->getBuilder('bar', null, null, array('multiple' => false))->setCompound(false)->getForm());
1043+
1044+
$this->form->submit(array(
1045+
'foo' => array('foo'),
1046+
'bar' => array('bar'),
1047+
));
1048+
1049+
$this->assertNull($this->form->get('foo')->getData());
1050+
$this->assertSame('Submitted data was expected to be text or number, array given.', $this->form->get('foo')->getTransformationFailure()->getMessage());
1051+
1052+
$this->assertSame(array('bar'), $this->form->get('bar')->getData());
1053+
}
1054+
10391055
public function testFileUpload()
10401056
{
10411057
$reqHandler = new HttpFoundationRequestHandler();

src/Symfony/Component/Form/Tests/Extension/Core/Type/UrlTypeTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,6 @@ public function testThrowExceptionIfDefaultProtocolIsInvalid()
8383
));
8484
}
8585

86-
public function testSubmitWithNonStringDataDoesNotBreakTheFixUrlProtocolListener()
87-
{
88-
$form = $this->factory->create(static::TESTED_TYPE);
89-
$form->submit(array('domain.com', 'www.domain.com'));
90-
91-
$this->assertSame(array('domain.com', 'www.domain.com'), $form->getData());
92-
}
93-
9486
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expectedData = 'http://empty')
9587
{
9688
$form = $this->factory->create(static::TESTED_TYPE, null, array(

src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public function testDontValidateConstraintsIfNoValidationGroups()
220220
->getForm();
221221

222222
// Launch transformer
223-
$form->submit(array());
223+
$form->submit('foo');
224224

225225
$this->expectNoValidate();
226226

0 commit comments

Comments
 (0)
0