diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index 9c4fe05195..cdf3c5dd0f 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/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,41 +726,10 @@ 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() - */ - public function bindGlobals($data = null) - { - $this->bind(Request::createFromGlobals(), $data); - } - /** * @var Boolean whether a request and an object were bound to the form */ @@ -768,6 +738,36 @@ class Form extends Field implements \IteratorAggregate, FormInterface return $this->bound; } + /** + * Validates the form and its domain object + * + * @throws FormException If the option "validator" was not set + */ + public function validate() + { + 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); + } + } + } + /** * @return true if this form is CSRF protected */ diff --git a/tests/Symfony/Tests/Component/Form/FormTest.php b/tests/Symfony/Tests/Component/Form/FormTest.php index 558d968c56..413fb35144 100644 --- a/tests/Symfony/Tests/Component/Form/FormTest.php +++ b/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();