Bladeren bron

[Form] Moved Form::validateData() to DelegatingValidator::validateFormData()

Bernhard Schussek 14 jaren geleden
bovenliggende
commit
3c412ffba0

+ 0 - 44
src/Symfony/Component/Form/Form.php

@@ -13,7 +13,6 @@ namespace Symfony\Component\Form;
 
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\FileBag;
-use Symfony\Component\Validator\ExecutionContext;
 use Symfony\Component\Form\Event\DataEvent;
 use Symfony\Component\Form\Event\FilterDataEvent;
 use Symfony\Component\Form\Exception\FormException;
@@ -781,47 +780,4 @@ class Form implements \IteratorAggregate, FormInterface
 
         $this->bind($data);
     }
-
-    /**
-     * Validates the data of this form
-     *
-     * This method is called automatically during the validation process.
-     *
-     * @param ExecutionContext $context  The current validation context
-     * @deprecated
-     */
-    public function validateData(ExecutionContext $context)
-    {
-        if (is_object($this->getData()) || is_array($this->getData())) {
-            $groups = $this->getAttribute('validation_groups');
-            $child = $this;
-
-            while (!$groups && $child->hasParent()) {
-                $child = $child->getParent();
-                $groups = $child->getAttribute('validation_groups');
-            }
-
-            if (null === $groups) {
-                $groups = array('Default');
-            }
-
-            $propertyPath = $context->getPropertyPath();
-            $graphWalker = $context->getGraphWalker();
-
-            // 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 .= '.';
-            }
-
-            $propertyPath .= 'data';
-
-            foreach ($groups as $group) {
-                $graphWalker->walkReference($this->getData(), $group, $propertyPath, true);
-            }
-        }
-    }
 }

+ 6 - 1
src/Symfony/Component/Form/Resources/config/validation.xml

@@ -5,7 +5,12 @@
     xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
 
   <class name="Symfony\Component\Form\Form">
-    <constraint name="Callback"><value>validateData</value></constraint>
+    <constraint name="Callback">
+      <value>
+        <value>Symfony\Component\Form\Validator\DelegatingValidator</value>
+        <value>validateFormData</value>
+      </value>
+    </constraint>
     <property name="children">
       <constraint name="Valid" />
     </property>

+ 50 - 0
src/Symfony/Component/Form/Validator/DelegatingValidator.php

@@ -16,6 +16,7 @@ use Symfony\Component\Form\FormError;
 use Symfony\Component\Form\VirtualFormIterator;
 use Symfony\Component\Form\Exception\FormException;
 use Symfony\Component\Validator\ValidatorInterface;
+use Symfony\Component\Validator\ExecutionContext;
 
 class DelegatingValidator implements FormValidatorInterface
 {
@@ -166,4 +167,53 @@ class DelegatingValidator implements FormValidatorInterface
             }
         }
     }
+
+    /**
+     * Validates the data of a form
+     *
+     * This method is called automatically during the validation process.
+     *
+     * @param FormInterface $form        The validated form
+     * @param ExecutionContext $context  The current validation context
+     */
+    public static function validateFormData(FormInterface $form, ExecutionContext $context)
+    {
+        if (is_object($form->getData()) || is_array($form->getData())) {
+            $groups = null;
+
+            $child = $form;
+            if ($form->hasAttribute('validation_groups')) {
+                $groups = $form->getAttribute('validation_groups');
+            }
+
+            while (!$groups && $child->hasParent()) {
+                $child = $child->getParent();
+                if ($form->hasAttribute('validation_groups')) {
+                    $groups = $form->getAttribute('validation_groups');
+                }
+            }
+
+            if (null === $groups) {
+                $groups = array('Default');
+            }
+
+            $propertyPath = $context->getPropertyPath();
+            $graphWalker = $context->getGraphWalker();
+
+            // 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 .= '.';
+            }
+
+            $propertyPath .= 'data';
+
+            foreach ($groups as $group) {
+                $graphWalker->walkReference($form->getData(), $group, $propertyPath, true);
+            }
+        }
+    }
 }

+ 0 - 93
tests/Symfony/Tests/Component/Form/Type/FormTypeTest.php

@@ -451,87 +451,6 @@ class FormTest extends TestCase
         $this->assertEquals($object, $form->getData());
     }
 
-    public function testValidateData()
-    {
-        $graphWalker = $this->getMockGraphWalker();
-        $metadataFactory = $this->getMockMetadataFactory();
-        $context = new ExecutionContext('Root', $graphWalker, $metadataFactory);
-        $object = $this->getMock('\stdClass');
-        $form = $this->factory->create('form', 'author', array('validation_groups' => array(
-            'group1',
-            'group2',
-        )));
-
-        $graphWalker->expects($this->exactly(2))
-        ->method('walkReference')
-        ->with($object,
-        // should test for groups - PHPUnit limitation
-        $this->anything(),
-                'data',
-        true);
-
-        $form->setData($object);
-        $form->validateData($context);
-    }
-
-    public function testValidateDataAppendsPropertyPath()
-    {
-        $graphWalker = $this->getMockGraphWalker();
-        $metadataFactory = $this->getMockMetadataFactory();
-        $context = new ExecutionContext('Root', $graphWalker, $metadataFactory);
-        $context->setPropertyPath('path');
-        $object = $this->getMock('\stdClass');
-        $form = $this->factory->create('form', 'author');
-
-        $graphWalker->expects($this->once())
-            ->method('walkReference')
-            ->with($object, 'Default', 'path.data', true);
-
-        $form->setData($object);
-        $form->validateData($context);
-    }
-
-    public function testValidateDataSetsCurrentPropertyToData()
-    {
-        $graphWalker = $this->getMockGraphWalker();
-        $metadataFactory = $this->getMockMetadataFactory();
-        $context = new ExecutionContext('Root', $graphWalker, $metadataFactory);
-        $object = $this->getMock('\stdClass');
-        $form = $this->factory->create('form', 'author');
-        $test = $this;
-
-        $graphWalker->expects($this->once())
-        ->method('walkReference')
-        ->will($this->returnCallback(function () use ($context, $test) {
-            $test->assertEquals('data', $context->getCurrentProperty());
-        }));
-
-        $form->setData($object);
-        $form->validateData($context);
-    }
-
-    public function testValidateDataDoesNotWalkScalars()
-    {
-        $graphWalker = $this->getMockGraphWalker();
-        $metadataFactory = $this->getMockMetadataFactory();
-        $context = new ExecutionContext('Root', $graphWalker, $metadataFactory);
-        $clientTransformer = $this->getMockTransformer();
-
-        $builder = $this->factory->createBuilder('form', 'author');
-        $builder->setClientTransformer($clientTransformer);
-        $form = $builder->getForm();
-
-        $graphWalker->expects($this->never())
-        ->method('walkReference');
-
-        $clientTransformer->expects($this->atLeastOnce())
-        ->method('reverseTransform')
-        ->will($this->returnValue('foobar'));
-
-        $form->bind(array('foo' => 'bar')); // reverse transformed to "foobar"
-        $form->validateData($context);
-    }
-
     public function testSubformDoesntCallSetters()
     {
         $author = new FormTest_AuthorWithoutRefSetter(new Author());
@@ -718,18 +637,6 @@ class FormTest extends TestCase
         return $this->getMock('Symfony\Component\Form\CsrfProvider\CsrfProviderInterface');
     }
 
-    protected function getMockGraphWalker()
-    {
-        return $this->getMockBuilder('Symfony\Component\Validator\GraphWalker')
-        ->disableOriginalConstructor()
-        ->getMock();
-    }
-
-    protected function getMockMetadataFactory()
-    {
-        return $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
-    }
-
     protected function getPostRequest(array $values = array(), array $files = array())
     {
         $server = array('REQUEST_METHOD' => 'POST');

+ 107 - 7
tests/Symfony/Tests/Component/Form/Validator/DelegatingValidatorTest.php

@@ -16,6 +16,7 @@ use Symfony\Component\Form\FormError;
 use Symfony\Component\Form\PropertyPath;
 use Symfony\Component\Form\Validator\DelegatingValidator;
 use Symfony\Component\Validator\ConstraintViolation;
+use Symfony\Component\Validator\ExecutionContext;
 
 class DelegatingValidatorTest extends \PHPUnit_Framework_TestCase
 {
@@ -40,6 +41,22 @@ class DelegatingValidatorTest extends \PHPUnit_Framework_TestCase
         $this->params = array('foo' => 'bar');
     }
 
+    protected function getMockGraphWalker()
+    {
+        return $this->getMockBuilder('Symfony\Component\Validator\GraphWalker')
+            ->disableOriginalConstructor()
+            ->getMock();
+    }
+
+    protected function getMockMetadataFactory()
+    {
+        return $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
+    }
+
+    protected function getMockTransformer()
+    {
+        return $this->getMock('Symfony\Component\Form\DataTransformer\DataTransformerInterface', array(), array(), '', false, false);
+    }
 
     protected function getConstraintViolation($propertyPath)
     {
@@ -63,7 +80,7 @@ class DelegatingValidatorTest extends \PHPUnit_Framework_TestCase
 
     protected function getForm($name, $propertyPath = null)
     {
-        return $this->getBuilder($name, $propertyPath)->getForm();
+        return $this->getBuilder($name, $propertyPath)->getForm('author');
     }
 
     public function testFormErrorsOnForm()
@@ -282,7 +299,7 @@ class DelegatingValidatorTest extends \PHPUnit_Framework_TestCase
             ->setAttribute('error_mapping', array(
                 'passwordPlain' => 'password',
             ))
-            ->getForm();
+            ->getForm('author');
         $child = $this->getForm('password');
 
         $parent->add($child);
@@ -305,7 +322,7 @@ class DelegatingValidatorTest extends \PHPUnit_Framework_TestCase
             ->setAttribute('error_mapping', array(
                 'address.streetName' => 'address.street',
             ))
-            ->getForm();
+            ->getForm('author');
         $child = $this->getForm('address');
         $grandChild = $this->getForm('street');
 
@@ -332,7 +349,7 @@ class DelegatingValidatorTest extends \PHPUnit_Framework_TestCase
             ->setAttribute('error_mapping', array(
                 'streetName' => 'street',
             ))
-            ->getForm();
+            ->getForm('author');
         $grandChild = $this->getForm('street');
 
         $parent->add($child);
@@ -358,7 +375,7 @@ class DelegatingValidatorTest extends \PHPUnit_Framework_TestCase
             ->setAttribute('error_mapping', array(
                 'streetName' => 'street',
             ))
-            ->getForm();
+            ->getForm('author');
         $grandChild = $this->getForm('street');
 
         $parent->add($child);
@@ -383,10 +400,10 @@ class DelegatingValidatorTest extends \PHPUnit_Framework_TestCase
             ->setAttribute('error_mapping', array(
                 'streetName' => 'street',
             ))
-            ->getForm();
+            ->getForm('author');
         $child = $this->getBuilder('address')
             ->setAttribute('virtual', true)
-            ->getForm();
+            ->getForm('author');
         $grandChild = $this->getForm('street');
 
         $parent->add($child);
@@ -404,4 +421,87 @@ class DelegatingValidatorTest extends \PHPUnit_Framework_TestCase
         $this->assertFalse($child->hasErrors());
         $this->assertEquals(array($this->getFormError()), $grandChild->getErrors());
     }
+
+    public function testValidateFormData()
+    {
+        $graphWalker = $this->getMockGraphWalker();
+        $metadataFactory = $this->getMockMetadataFactory();
+        $context = new ExecutionContext('Root', $graphWalker, $metadataFactory);
+        $object = $this->getMock('\stdClass');
+        $form = $this->getBuilder('author')
+            ->setAttribute('validation_groups', array('group1', 'group2'))
+            ->getForm('author');
+
+        $graphWalker->expects($this->at(0))
+            ->method('walkReference')
+            ->with($object, 'group1', 'data', true);
+        $graphWalker->expects($this->at(1))
+            ->method('walkReference')
+            ->with($object, 'group2', 'data', true);
+
+        $form->setData($object);
+
+        DelegatingValidator::validateFormData($form, $context);
+    }
+
+    public function testValidateFormDataAppendsPropertyPath()
+    {
+        $graphWalker = $this->getMockGraphWalker();
+        $metadataFactory = $this->getMockMetadataFactory();
+        $context = new ExecutionContext('Root', $graphWalker, $metadataFactory);
+        $context->setPropertyPath('path');
+        $object = $this->getMock('\stdClass');
+        $form = $this->getForm('author');
+
+        $graphWalker->expects($this->once())
+            ->method('walkReference')
+            ->with($object, 'Default', 'path.data', true);
+
+        $form->setData($object);
+
+        DelegatingValidator::validateFormData($form, $context);
+    }
+
+    public function testValidateFormDataSetsCurrentPropertyToData()
+    {
+        $graphWalker = $this->getMockGraphWalker();
+        $metadataFactory = $this->getMockMetadataFactory();
+        $context = new ExecutionContext('Root', $graphWalker, $metadataFactory);
+        $object = $this->getMock('\stdClass');
+        $form = $this->getForm('author');
+        $test = $this;
+
+        $graphWalker->expects($this->once())
+            ->method('walkReference')
+            ->will($this->returnCallback(function () use ($context, $test) {
+                $test->assertEquals('data', $context->getCurrentProperty());
+            }));
+
+        $form->setData($object);
+
+        DelegatingValidator::validateFormData($form, $context);
+    }
+
+    public function testValidateFormDataDoesNotWalkScalars()
+    {
+        $graphWalker = $this->getMockGraphWalker();
+        $metadataFactory = $this->getMockMetadataFactory();
+        $context = new ExecutionContext('Root', $graphWalker, $metadataFactory);
+        $clientTransformer = $this->getMockTransformer();
+
+        $form = $this->getBuilder('author')
+            ->setClientTransformer($clientTransformer)
+            ->getForm('author');
+
+        $graphWalker->expects($this->never())
+            ->method('walkReference');
+
+        $clientTransformer->expects($this->atLeastOnce())
+            ->method('reverseTransform')
+            ->will($this->returnValue('foobar'));
+
+        $form->bind(array('foo' => 'bar')); // reverse transformed to "foobar"
+
+        DelegatingValidator::validateFormData($form, $context);
+    }
 }