Просмотр исходного кода

[Form] Refactored validation logic into validate() method. Removed bindGlobals() to reduce API clutter

Bernhard Schussek 14 лет назад
Родитель
Сommit
628a4d1fd8
2 измененных файлов с 31 добавлено и 68 удалено
  1. 30 30
      src/Symfony/Component/Form/Form.php
  2. 1 38
      tests/Symfony/Tests/Component/Form/FormTest.php

+ 30 - 30
src/Symfony/Component/Form/Form.php

@@ -15,6 +15,7 @@ use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\FileBag;
 use Symfony\Component\Validator\ValidatorInterface;
 use Symfony\Component\Form\Exception\FormException;
+use Symfony\Component\Form\Exception\MissingOptionsException;
 use Symfony\Component\Form\Exception\AlreadyBoundException;
 use Symfony\Component\Form\Exception\UnexpectedTypeException;
 use Symfony\Component\Form\Exception\DanglingFieldException;
@@ -725,47 +726,46 @@ class Form extends Field implements \IteratorAggregate, FormInterface
 
             $this->submit(self::deepArrayUnion($values, $files));
 
-            if (null === $this->getOption('validator')) {
-                throw new FormException('A validator is required for binding. Forgot to pass it to the constructor of the form?');
-            }
-
-            // Validate the submitted data
-            if ($violations = $this->getOption('validator')->validate($this, $this->getOption('validation_groups'))) {
-                // TODO: test me
-                foreach ($violations as $violation) {
-                    $propertyPath = new PropertyPath($violation->getPropertyPath());
-                    $iterator = $propertyPath->getIterator();
-
-                    if ($iterator->current() == 'data') {
-                        $type = self::DATA_ERROR;
-                        $iterator->next(); // point at the first data element
-                    } else {
-                        $type = self::FIELD_ERROR;
-                    }
-
-                    $this->addError(new FieldError($violation->getMessageTemplate(), $violation->getMessageParameters()), $iterator, $type);
-                }
-            }
+            $this->validate();
         }
     }
 
     /**
-     * Binds the form with submitted data from the PHP globals $_POST and
-     * $_FILES
-     *
-     * @see bind()
+     * @var Boolean  whether a request and an object were bound to the form
      */
-    public function bindGlobals($data = null)
+    public function isBound()
     {
-        $this->bind(Request::createFromGlobals(), $data);
+        return $this->bound;
     }
 
     /**
-     * @var Boolean  whether a request and an object were bound to the form
+     * Validates the form and its domain object
+     *
+     * @throws FormException  If the option "validator" was not set
      */
-    public function isBound()
+    public function validate()
     {
-        return $this->bound;
+        if (null === $this->getOption('validator')) {
+            throw new MissingOptionsException('The option "validator" is required for validating', array('validator'));
+        }
+
+        // Validate the submitted data
+        if ($violations = $this->getOption('validator')->validate($this, $this->getOption('validation_groups'))) {
+            // TODO: test me
+            foreach ($violations as $violation) {
+                $propertyPath = new PropertyPath($violation->getPropertyPath());
+                $iterator = $propertyPath->getIterator();
+
+                if ($iterator->current() == 'data') {
+                    $type = self::DATA_ERROR;
+                    $iterator->next(); // point at the first data element
+                } else {
+                    $type = self::FIELD_ERROR;
+                }
+
+                $this->addError(new FieldError($violation->getMessageTemplate(), $violation->getMessageParameters()), $iterator, $type);
+            }
+        }
     }
 
     /**

+ 1 - 38
tests/Symfony/Tests/Component/Form/FormTest.php

@@ -220,7 +220,7 @@ class FormTest extends \PHPUnit_Framework_TestCase
         $form = new Form('author');
         $form->add($field);
 
-        $this->setExpectedException('Symfony\Component\Form\Exception\FormException');
+        $this->setExpectedException('Symfony\Component\Form\Exception\MissingOptionsException');
 
         // data is irrelevant
         $form->bind($this->createPostRequest());
@@ -270,43 +270,6 @@ class FormTest extends \PHPUnit_Framework_TestCase
         $this->assertSame($object, $form->getData());
     }
 
-    public function testBindGlobals()
-    {
-        $_POST = array(
-            'author' => array(
-                'name' => 'Bernhard',
-                'image' => array('filename' => 'foobar.png'),
-            ),
-        );
-        $_FILES = array(
-            'author' => array(
-                'error' => array('image' => array('file' => UPLOAD_ERR_OK)),
-                'name' => array('image' => array('file' => 'upload.png')),
-                'size' => array('image' => array('file' => 123)),
-                'tmp_name' => array('image' => array('file' => 'abcdef.png')),
-                'type' => array('image' => array('file' => 'image/png')),
-            ),
-        );
-        // don't erase other variables
-        $_SERVER['REQUEST_METHOD'] = 'POST';
-
-
-        $form = new Form('author', array('validator' => $this->validator));
-        $form->add(new TestField('name'));
-        $imageForm = new Form('image');
-        $imageForm->add(new TestField('file'));
-        $imageForm->add(new TestField('filename'));
-        $form->add($imageForm);
-
-        $form->bindGlobals();
-
-        $file = new UploadedFile('abcdef.png', 'upload.png', 'image/png', 123, UPLOAD_ERR_OK);
-
-        $this->assertEquals('Bernhard', $form['name']->getData());
-        $this->assertEquals('foobar.png', $form['image']['filename']->getData());
-        $this->assertEquals($file, $form['image']['file']->getData());
-    }
-
     public function testReadPropertyIsIgnoredIfPropertyPathIsNull()
     {
         $author = new Author();