diff --git a/src/Symfony/Component/Form/Field.php b/src/Symfony/Component/Form/Field.php index f535ebd32d45..70fe34868202 100644 --- a/src/Symfony/Component/Form/Field.php +++ b/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 @@ public function bind($taintedData) $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 @@ public function isBound() 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. * diff --git a/src/Symfony/Component/Form/Resources/config/validation.xml b/src/Symfony/Component/Form/Resources/config/validation.xml index 6956ef9eacf0..c4ee988a8574 100644 --- a/src/Symfony/Component/Form/Resources/config/validation.xml +++ b/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"> + + + + + + + + diff --git a/tests/Symfony/Tests/Component/Form/FieldTest.php b/tests/Symfony/Tests/Component/Form/FieldTest.php index b0892602c09c..71e2914a6adb 100644 --- a/tests/Symfony/Tests/Component/Form/FieldTest.php +++ b/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\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 @@ public function testUpdatePropertyDoesNotUpdatePropertyIfPropertyPathIsEmpty() $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);