From f06203a640c1c0da89e7e0fa42fbc486800f0e32 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Wed, 11 Jul 2012 16:46:06 +0200 Subject: [PATCH 1/2] [Form] Improved ValidatorTypeGuesser to interpret the constraints True and False --- .../Extension/Validator/ValidatorTypeGuesser.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php b/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php index f80b818e8f..9b755e1682 100755 --- a/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php +++ b/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php @@ -171,7 +171,13 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface case 'Symfony\Component\Validator\Constraints\Url': return new TypeGuess('url', array(), Guess::HIGH_CONFIDENCE); + + case 'Symfony\Component\Validator\Constraints\True': + case 'Symfony\Component\Validator\Constraints\False': + return new TypeGuess('checkbox', array(), Guess::MEDIUM_CONFIDENCE); } + + return null; } /** @@ -186,8 +192,11 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface switch (get_class($constraint)) { case 'Symfony\Component\Validator\Constraints\NotNull': case 'Symfony\Component\Validator\Constraints\NotBlank': + case 'Symfony\Component\Validator\Constraints\True': return new ValueGuess(true, Guess::HIGH_CONFIDENCE); } + + return null; } /** @@ -215,6 +224,8 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface case 'Symfony\Component\Validator\Constraints\Size': return new ValueGuess(strlen((string) $constraint->max), Guess::LOW_CONFIDENCE); } + + return null; } /** @@ -250,6 +261,8 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface } break; } + + return null; } /** From c919b81ca96fb95e062c4a094d2d91aa7458dcaf Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Wed, 11 Jul 2012 16:49:59 +0200 Subject: [PATCH 2/2] [Form] Fixed TransformationFailedExceptions to be caught in the model transformers --- src/Symfony/Component/Form/CHANGELOG.md | 1 + src/Symfony/Component/Form/Form.php | 7 +++---- .../Component/Form/Tests/SimpleFormTest.php | 18 +++++++++++++++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index e0e783c5f7..028bf20a8d 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -156,3 +156,4 @@ CHANGELOG consumed by HTML5 browsers, if the widget is "single_text" * deprecated the options "data_timezone" and "user_timezone" in DateType, DateTimeType and TimeType and renamed them to "model_timezone" and "view_timezone" + * fixed: TransformationFailedExceptions thrown in the model transformer are now caught by the form diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index 2a5f046b95..f9c56e377b 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -544,11 +544,7 @@ class Form implements \IteratorAggregate, FormInterface try { // Normalize data to unified representation $normData = $this->viewToNorm($viewData); - $synchronized = true; - } catch (TransformationFailedException $e) { - } - if ($synchronized) { // Hook to change content of the data into the normalized // representation $event = new FormEvent($this, $normData); @@ -560,6 +556,9 @@ class Form implements \IteratorAggregate, FormInterface // Synchronize representations - must not change the content! $modelData = $this->normToModel($normData); $viewData = $this->normToView($normData); + + $synchronized = true; + } catch (TransformationFailedException $e) { } $this->bound = true; diff --git a/src/Symfony/Component/Form/Tests/SimpleFormTest.php b/src/Symfony/Component/Form/Tests/SimpleFormTest.php index d10d1dc9a3..12a16fc28a 100644 --- a/src/Symfony/Component/Form/Tests/SimpleFormTest.php +++ b/src/Symfony/Component/Form/Tests/SimpleFormTest.php @@ -480,7 +480,7 @@ class SimpleFormTest extends AbstractFormTest $this->assertTrue($this->form->isSynchronized()); } - public function testNotSynchronizedIfTransformationFailed() + public function testNotSynchronizedIfViewReverseTransformationFailed() { $transformer = $this->getDataTransformer(); $transformer->expects($this->once()) @@ -496,6 +496,22 @@ class SimpleFormTest extends AbstractFormTest $this->assertFalse($form->isSynchronized()); } + public function testNotSynchronizedIfModelReverseTransformationFailed() + { + $transformer = $this->getDataTransformer(); + $transformer->expects($this->once()) + ->method('reverseTransform') + ->will($this->throwException(new TransformationFailedException())); + + $form = $this->getBuilder() + ->addModelTransformer($transformer) + ->getForm(); + + $form->bind('foobar'); + + $this->assertFalse($form->isSynchronized()); + } + public function testEmptyDataCreatedBeforeTransforming() { $form = $this->getBuilder()