diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index 8b15447d1b27a..8f86ea0a08b95 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -532,6 +532,9 @@ public function submit($submittedData, $clearMissing = true) $submittedData = null; } elseif (is_scalar($submittedData)) { $submittedData = (string) $submittedData; + } 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(); @@ -541,6 +544,10 @@ public function submit($submittedData, $clearMissing = true) $viewData = null; try { + if (null !== $this->transformationFailure) { + throw $this->transformationFailure; + } + // Hook to change content of the data submitted by the browser if ($dispatcher->hasListeners(FormEvents::PRE_SUBMIT)) { $event = new FormEvent($this, $submittedData); diff --git a/src/Symfony/Component/Form/Tests/CompoundFormTest.php b/src/Symfony/Component/Form/Tests/CompoundFormTest.php index 336593f7f7639..51e9fe60bad64 100644 --- a/src/Symfony/Component/Form/Tests/CompoundFormTest.php +++ b/src/Symfony/Component/Form/Tests/CompoundFormTest.php @@ -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()); + } + protected function createForm() { return $this->getBuilder() diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/UrlTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/UrlTypeTest.php index a72bc985cda7e..c80a8b7cfb2e5 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/UrlTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/UrlTypeTest.php @@ -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( diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php index 1d277f6ffcc12..4003ccc01edd1 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php @@ -220,7 +220,7 @@ public function testDontValidateConstraintsIfNoValidationGroups() ->getForm(); // Launch transformer - $form->submit(array()); + $form->submit('foo'); $this->expectNoValidate();