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

Skip to content

Commit 60faaf1

Browse files
[Form] Filter arrays out of scalar form types
1 parent e4a7fd8 commit 60faaf1

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

src/Symfony/Component/Form/Form.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,9 @@ public function submit($submittedData, $clearMissing = true)
537537
$submittedData = null;
538538
} elseif (is_scalar($submittedData)) {
539539
$submittedData = (string) $submittedData;
540+
} elseif (\is_array($submittedData) && !$this->config->getCompound() && !$this->config->hasOption('multiple')) {
541+
$submittedData = null;
542+
$this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, array given.');
540543
}
541544

542545
$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
@@ -1088,6 +1088,22 @@ public function testDisabledButtonIsNotSubmitted()
10881088
$this->assertFalse($submit->isSubmitted());
10891089
}
10901090

1091+
public function testArrayTransformationFailuresOnSubmit()
1092+
{
1093+
$this->form->add($this->getBuilder('foo')->setCompound(false)->getForm());
1094+
$this->form->add($this->getBuilder('bar', null, null, array('multiple' => false))->setCompound(false)->getForm());
1095+
1096+
$this->form->submit(array(
1097+
'foo' => array('foo'),
1098+
'bar' => array('bar'),
1099+
));
1100+
1101+
$this->assertNull($this->form->get('foo')->getData());
1102+
$this->assertSame('Submitted data was expected to be text or number, array given.', $this->form->get('foo')->getTransformationFailure()->getMessage());
1103+
1104+
$this->assertSame(array('bar'), $this->form->get('bar')->getData());
1105+
}
1106+
10911107
protected function createForm()
10921108
{
10931109
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
@@ -93,14 +93,6 @@ public function testThrowExceptionIfDefaultProtocolIsInvalid()
9393
));
9494
}
9595

96-
public function testSubmitWithNonStringDataDoesNotBreakTheFixUrlProtocolListener()
97-
{
98-
$form = $this->factory->create(static::TESTED_TYPE);
99-
$form->submit(array('domain.com', 'www.domain.com'));
100-
101-
$this->assertSame(array('domain.com', 'www.domain.com'), $form->getData());
102-
}
103-
10496
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expectedData = 'http://empty')
10597
{
10698
$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
@@ -248,7 +248,7 @@ public function testDontValidateConstraintsIfNoValidationGroups()
248248
->getForm();
249249

250250
// Launch transformer
251-
$form->submit(array());
251+
$form->submit('foo');
252252

253253
$this->expectNoValidate();
254254

0 commit comments

Comments
 (0)
0