Browse Source

[Form] added the constrained method Field::isTransformationSuccessful()

Bernhard Schussek 14 years ago
parent
commit
e9a7531a26

+ 13 - 3
src/Symfony/Component/Form/Field.php

@@ -62,6 +62,7 @@ class Field extends Configurable implements FieldInterface
     private $normalizationTransformer = null;
     private $valueTransformer = null;
     private $propertyPath = null;
+    private $transformationSuccessful = true;
 
     public function __construct($key, array $options = array())
     {
@@ -269,10 +270,9 @@ class Field extends Configurable implements FieldInterface
             $this->normalizedData = $this->processData($this->reverseTransform($this->transformedData));
             $this->data = $this->denormalize($this->normalizedData);
             $this->transformedData = $this->transform($this->normalizedData);
+            $this->transformationSuccessful = true;
         } catch (TransformationFailedException $e) {
-            // TODO better text
-            // TESTME
-            $this->addError(new FieldError('invalid (localized)'));
+            $this->transformationSuccessful = false;
         }
     }
 
@@ -329,6 +329,16 @@ class Field extends Configurable implements FieldInterface
         return $this->bound;
     }
 
+    /**
+     * Returns whether the bound value could be reverse transformed correctly
+     *
+     * @return boolean
+     */
+    public function isTransformationSuccessful()
+    {
+        return $this->transformationSuccessful;
+    }
+
     /**
      * Returns whether the field is valid.
      *

+ 8 - 0
src/Symfony/Component/Form/Resources/config/validation.xml

@@ -4,6 +4,14 @@
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.symfony-project.org/schema/dic/constraint-mapping http://www.symfony-project.org/schema/dic/services/constraint-mapping-1.0.xsd">
 
+  <class name="Symfony\Component\Form\Field">
+    <getter property="transformationSuccessful">
+      <constraint name="AssertTrue">
+        <option name="message">This value is invalid</option>
+      </constraint>
+    </getter>
+  </class>
+
   <class name="Symfony\Component\Form\FieldGroup">
     <property name="fields">
       <constraint name="Valid" />

+ 33 - 0
tests/Symfony/Tests/Component/Form/FieldTest.php

@@ -2,6 +2,7 @@
 
 namespace Symfony\Tests\Component\Form;
 
+
 require_once __DIR__ . '/Fixtures/Author.php';
 require_once __DIR__ . '/Fixtures/TestField.php';
 require_once __DIR__ . '/Fixtures/InvalidField.php';
@@ -11,6 +12,7 @@ use Symfony\Component\Form\ValueTransformer\ValueTransformerInterface;
 use Symfony\Component\Form\PropertyPath;
 use Symfony\Component\Form\FieldError;
 use Symfony\Component\Form\FormConfiguration;
+use Symfony\Component\Form\ValueTransformer\TransformationFailedException;
 use Symfony\Tests\Component\Form\Fixtures\Author;
 use Symfony\Tests\Component\Form\Fixtures\TestField;
 use Symfony\Tests\Component\Form\Fixtures\InvalidField;
@@ -450,6 +452,37 @@ class FieldTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals(null, $object->firstName);
     }
 
+    public function testIsTransformationSuccessfulReturnsTrueIfReverseTransformSucceeded()
+    {
+        $field = new TestField('title', array(
+            'trim' => false,
+        ));
+
+        $field->bind('a');
+
+        $this->assertEquals('a', $field->getDisplayedData());
+        $this->assertTrue($field->isTransformationSuccessful());
+    }
+
+    public function testIsTransformationSuccessfulReturnsFalseIfReverseTransformThrowsException()
+    {
+        // The value is passed to the value transformer
+        $transformer = $this->createMockTransformer();
+        $transformer->expects($this->once())
+                ->method('reverseTransform')
+                ->will($this->throwException(new TransformationFailedException()));
+
+        $field = new TestField('title', array(
+            'trim' => false,
+            'value_transformer' => $transformer,
+        ));
+
+        $field->bind('a');
+
+        $this->assertEquals('a', $field->getDisplayedData());
+        $this->assertFalse($field->isTransformationSuccessful());
+    }
+
     protected function createMockTransformer()
     {
         return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);