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

Skip to content

Commit d7faca2

Browse files
[Form] Filter arrays out of scalar form types
1 parent f747ea9 commit d7faca2

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

src/Symfony/Component/Form/Form.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,9 @@ public function submit($submittedData, $clearMissing = true)
532532
$submittedData = null;
533533
} elseif (is_scalar($submittedData)) {
534534
$submittedData = (string) $submittedData;
535+
} elseif (\is_array($submittedData) && !$this->config->getCompound() && !$this->config->hasOption('multiple')) {
536+
$submittedData = null;
537+
$this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, array given.');
535538
}
536539

537540
$dispatcher = $this->config->getEventDispatcher();
@@ -541,6 +544,10 @@ public function submit($submittedData, $clearMissing = true)
541544
$viewData = null;
542545

543546
try {
547+
if (null !== $this->transformationFailure) {
548+
throw $this->transformationFailure;
549+
}
550+
544551
// Hook to change content of the data submitted by the browser
545552
if ($dispatcher->hasListeners(FormEvents::PRE_SUBMIT)) {
546553
$event = new FormEvent($this, $submittedData);

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
protected function createForm()
10401056
{
10411057
return $this->getBuilder()

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