Browse Source

[Form] Removed obsolete classes Error and DataError

Bernhard Schussek 14 years ago
parent
commit
3985522b9b

+ 0 - 21
src/Symfony/Component/Form/DataError.php

@@ -1,21 +0,0 @@
-<?php
-
-/*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace Symfony\Component\Form;
-
-/**
- * Wraps errors in the form data
- *
- * @author Bernhard Schussek <bernhard.schussek@symfony.com>
- */
-class DataError extends Error
-{
-}

+ 0 - 65
src/Symfony/Component/Form/Error.php

@@ -1,65 +0,0 @@
-<?php
-
-/*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace Symfony\Component\Form;
-
-/**
- * Wraps errors in forms
- *
- * @author Bernhard Schussek <bernhard.schussek@symfony.com>
- */
-abstract class Error
-{
-    /**
-     * The template for the error message
-     * @var string
-     */
-    protected $messageTemplate;
-
-    /**
-     * The parameters that should be substituted in the message template
-     * @var array
-     */
-    protected $messageParameters;
-
-    /**
-     * Constructor
-     *
-     * @param string $messageTemplate      The template for the error message
-     * @param array $messageParameters     The parameters that should be
-     *                                     substituted in the message template.
-     */
-    public function __construct($messageTemplate, array $messageParameters = array())
-    {
-        $this->messageTemplate = $messageTemplate;
-        $this->messageParameters = $messageParameters;
-    }
-
-    /**
-     * Returns the error message template
-     *
-     * @return string
-     */
-    public function getMessageTemplate()
-    {
-        return $this->messageTemplate;
-    }
-
-    /**
-     * Returns the parameters to be inserted in the message template
-     *
-     * @return array
-     */
-    public function getMessageParameters()
-    {
-        return $this->messageParameters;
-    }
-}

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

@@ -412,7 +412,7 @@ class Form implements \IteratorAggregate, FormInterface
      *
      * @see FormInterface
      */
-    public function addError(Error $error)
+    public function addError(FormError $error)
     {
         $this->errors[] = $error;
     }

+ 46 - 2
src/Symfony/Component/Form/FormError.php

@@ -12,10 +12,54 @@
 namespace Symfony\Component\Form;
 
 /**
- * Wraps errors in form fields
+ * Wraps errors in forms
  *
  * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  */
-class FormError extends Error
+class FormError
 {
+    /**
+     * The template for the error message
+     * @var string
+     */
+    protected $messageTemplate;
+
+    /**
+     * The parameters that should be substituted in the message template
+     * @var array
+     */
+    protected $messageParameters;
+
+    /**
+     * Constructor
+     *
+     * @param string $messageTemplate      The template for the error message
+     * @param array $messageParameters     The parameters that should be
+     *                                     substituted in the message template.
+     */
+    public function __construct($messageTemplate, array $messageParameters = array())
+    {
+        $this->messageTemplate = $messageTemplate;
+        $this->messageParameters = $messageParameters;
+    }
+
+    /**
+     * Returns the error message template
+     *
+     * @return string
+     */
+    public function getMessageTemplate()
+    {
+        return $this->messageTemplate;
+    }
+
+    /**
+     * Returns the parameters to be inserted in the message template
+     *
+     * @return array
+     */
+    public function getMessageParameters()
+    {
+        return $this->messageParameters;
+    }
 }

+ 2 - 2
src/Symfony/Component/Form/FormInterface.php

@@ -42,9 +42,9 @@ interface FormInterface extends \ArrayAccess, \Traversable, \Countable
     /**
      * Adds an error to this form
      *
-     * @param Error $error
+     * @param FormError $error
      */
-    function addError(Error $error);
+    function addError(FormError $error);
 
     /**
      * Returns whether the form is valid.

+ 24 - 21
src/Symfony/Component/Form/Validator/DelegatingValidator.php

@@ -12,8 +12,6 @@
 namespace Symfony\Component\Form\Validator;
 
 use Symfony\Component\Form\FormInterface;
-use Symfony\Component\Form\Error;
-use Symfony\Component\Form\DataError;
 use Symfony\Component\Form\FormError;
 use Symfony\Component\Form\PropertyPath;
 use Symfony\Component\Form\PropertyPathIterator;
@@ -21,6 +19,10 @@ use Symfony\Component\Validator\ValidatorInterface;
 
 class DelegatingValidator implements FormValidatorInterface
 {
+    const DATA_ERROR = 0;
+
+    const FORM_ERROR = 1;
+
     private $validator;
 
     public function __construct(ValidatorInterface $validator)
@@ -31,52 +33,53 @@ class DelegatingValidator implements FormValidatorInterface
     /**
      * Validates the form and its domain object
      */
-    public function validate(FormInterface $field)
+    public function validate(FormInterface $form)
     {
-        if ($field->isRoot()) {
-            // Validate the field in group "Default"
+        if ($form->isRoot()) {
+            // Validate the form in group "Default"
             // Validation of the data in the custom group is done by validateData(),
             // which is constrained by the Execute constraint
-            if ($violations = $this->validator->validate($field)) {
+            if ($violations = $this->validator->validate($form)) {
                 foreach ($violations as $violation) {
                     $propertyPath = new PropertyPath($violation->getPropertyPath());
                     $iterator = $propertyPath->getIterator();
                     $template = $violation->getMessageTemplate();
                     $parameters = $violation->getMessageParameters();
+                    $error = new FormError($template, $parameters);
 
                     if ($iterator->current() == 'data') {
                         $iterator->next(); // point at the first data element
-                        $error = new DataError($template, $parameters);
+                        $type = self::DATA_ERROR;
                     } else {
-                        $error = new FormError($template, $parameters);
+                        $type = self::FORM_ERROR;
                     }
 
-                    $this->mapError($error, $field, $iterator);
+                    $this->mapError($error, $form, $iterator, $type);
                 }
             }
         }
     }
 
-    private function mapError(Error $error, FormInterface $field,
-            PropertyPathIterator $pathIterator = null)
+    private function mapError(FormError $error, FormInterface $form,
+            PropertyPathIterator $pathIterator, $type)
     {
-        if (null !== $pathIterator && $field instanceof FormInterface) {
-            if ($error instanceof FormError && $pathIterator->hasNext()) {
+        if (null !== $pathIterator && $form instanceof FormInterface) {
+            if ($type === self::FORM_ERROR && $pathIterator->hasNext()) {
                 $pathIterator->next();
 
-                if ($pathIterator->isProperty() && $pathIterator->current() === 'fields') {
+                if ($pathIterator->isProperty() && $pathIterator->current() === 'forms') {
                     $pathIterator->next();
                 }
 
-                if ($field->has($pathIterator->current())) {
-                    $child = $field->get($pathIterator->current());
+                if ($form->has($pathIterator->current())) {
+                    $child = $form->get($pathIterator->current());
 
-                    $this->mapError($error, $child, $pathIterator);
+                    $this->mapError($error, $child, $pathIterator, $type);
 
                     return;
                 }
-            } else if ($error instanceof DataError) {
-                $iterator = new RecursiveFieldIterator($field);
+            } else if ($type === self::DATA_ERROR) {
+                $iterator = new RecursiveFieldIterator($form);
                 $iterator = new \RecursiveIteratorIterator($iterator);
 
                 foreach ($iterator as $child) {
@@ -86,7 +89,7 @@ class DelegatingValidator implements FormValidatorInterface
                                 $pathIterator->next();
                             }
 
-                            $this->mapError($error, $child, $pathIterator);
+                            $this->mapError($error, $child, $pathIterator, $type);
 
                             return;
                         }
@@ -95,6 +98,6 @@ class DelegatingValidator implements FormValidatorInterface
             }
         }
 
-        $field->addError($error);
+        $form->addError($error);
     }
 }