From 2276b98fc18c508fa0d41d7b7051caf4d703bbe5 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Fri, 4 Feb 2011 10:12:11 +0100 Subject: [PATCH] [Form] Fixed: ChoiceFields never validated --- src/Symfony/Component/Form/Form.php | 35 ++++++++++--------- .../Symfony/Tests/Component/Form/FormTest.php | 19 ++++++++++ 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index 14dc1f2d75..db1eb006ac 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -855,28 +855,29 @@ class Form extends Field implements \IteratorAggregate, FormInterface */ public function validateData(ExecutionContext $context) { - $groups = $this->getValidationGroups(); - $propertyPath = $context->getPropertyPath(); - $graphWalker = $context->getGraphWalker(); + if (is_object($this->getData()) || is_array($this->getData())) { + $groups = $this->getValidationGroups(); + $propertyPath = $context->getPropertyPath(); + $graphWalker = $context->getGraphWalker(); - if (null === $groups) { - $groups = array(null); - } + if (null === $groups) { + $groups = array(null); + } - // The Execute constraint is called on class level, so we need to - // set the property manually - $context->setCurrentProperty('data'); + // The Execute constraint is called on class level, so we need to + // set the property manually + $context->setCurrentProperty('data'); - // Adjust the property path accordingly - if (!empty($propertyPath)) { - $propertyPath .= '.'; - } + // Adjust the property path accordingly + if (!empty($propertyPath)) { + $propertyPath .= '.'; + } - $propertyPath .= 'data'; + $propertyPath .= 'data'; - foreach ($groups as $group) { - // Don't use potential overridden versions of getData()! - $graphWalker->walkReference(Form::getData(), $group, $propertyPath, true); + foreach ($groups as $group) { + $graphWalker->walkReference($this->getData(), $group, $propertyPath, true); + } } } diff --git a/tests/Symfony/Tests/Component/Form/FormTest.php b/tests/Symfony/Tests/Component/Form/FormTest.php index 7becaffe6d..128fd50f64 100644 --- a/tests/Symfony/Tests/Component/Form/FormTest.php +++ b/tests/Symfony/Tests/Component/Form/FormTest.php @@ -1111,6 +1111,25 @@ class FormTest extends \PHPUnit_Framework_TestCase $form->validateData($context); } + public function testValidateDataDoesNotWalkScalars() + { + $graphWalker = $this->createMockGraphWalker(); + $metadataFactory = $this->createMockMetadataFactory(); + $context = new ExecutionContext('Root', $graphWalker, $metadataFactory); + $valueTransformer = $this->createMockTransformer(); + $form = new Form('author', array('value_transformer' => $valueTransformer)); + + $graphWalker->expects($this->never()) + ->method('walkReference'); + + $valueTransformer->expects($this->atLeastOnce()) + ->method('reverseTransform') + ->will($this->returnValue('foobar')); + + $form->submit(array('foo' => 'bar')); // reverse transformed to "foobar" + $form->validateData($context); + } + /** * Create a group containing two fields, "visibleField" and "hiddenField" *