From 385d9df29cdf8a34df4d8f95258d97e85ca3dbae Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Thu, 4 Oct 2018 21:52:40 +0200 Subject: [PATCH] invalidate forms on transformation failures --- .../FrameworkBundle/Resources/config/form.xml | 5 ++ .../Form/Extension/Core/CoreExtension.php | 13 +++- .../TransformationFailureListener.php | 64 +++++++++++++++++++ .../Type/TransformationFailureExtension.php | 42 ++++++++++++ .../Extension/Core/CoreExtensionTest.php | 33 ++++++++++ 5 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/Form/Extension/Core/EventListener/TransformationFailureListener.php create mode 100644 src/Symfony/Component/Form/Extension/Core/Type/TransformationFailureExtension.php create mode 100644 src/Symfony/Component/Form/Tests/Extension/Core/CoreExtensionTest.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml index 7e86fb0728..b0bcf00443 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml @@ -166,6 +166,11 @@ + + + + + diff --git a/src/Symfony/Component/Form/Extension/Core/CoreExtension.php b/src/Symfony/Component/Form/Extension/Core/CoreExtension.php index fc9af48891..e2e40e4634 100644 --- a/src/Symfony/Component/Form/Extension/Core/CoreExtension.php +++ b/src/Symfony/Component/Form/Extension/Core/CoreExtension.php @@ -16,8 +16,10 @@ use Symfony\Component\Form\ChoiceList\Factory\CachingFactoryDecorator; use Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface; use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory; use Symfony\Component\Form\ChoiceList\Factory\PropertyAccessDecorator; +use Symfony\Component\Form\Extension\Core\Type\TransformationFailureExtension; use Symfony\Component\PropertyAccess\PropertyAccess; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; +use Symfony\Component\Translation\TranslatorInterface; /** * Represents the main form extension, which loads the core functionality. @@ -28,11 +30,13 @@ class CoreExtension extends AbstractExtension { private $propertyAccessor; private $choiceListFactory; + private $translator; - public function __construct(PropertyAccessorInterface $propertyAccessor = null, ChoiceListFactoryInterface $choiceListFactory = null) + public function __construct(PropertyAccessorInterface $propertyAccessor = null, ChoiceListFactoryInterface $choiceListFactory = null, TranslatorInterface $translator = null) { $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); $this->choiceListFactory = $choiceListFactory ?: new CachingFactoryDecorator(new PropertyAccessDecorator(new DefaultChoiceListFactory(), $this->propertyAccessor)); + $this->translator = $translator; } protected function loadTypes() @@ -71,4 +75,11 @@ class CoreExtension extends AbstractExtension new Type\CurrencyType(), ); } + + protected function loadTypeExtensions() + { + return array( + new TransformationFailureExtension($this->translator), + ); + } } diff --git a/src/Symfony/Component/Form/Extension/Core/EventListener/TransformationFailureListener.php b/src/Symfony/Component/Form/Extension/Core/EventListener/TransformationFailureListener.php new file mode 100644 index 0000000000..f46eb499e0 --- /dev/null +++ b/src/Symfony/Component/Form/Extension/Core/EventListener/TransformationFailureListener.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Extension\Core\EventListener; + +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\Form\FormError; +use Symfony\Component\Form\FormEvent; +use Symfony\Component\Form\FormEvents; +use Symfony\Component\Translation\TranslatorInterface; + +/** + * @author Christian Flothmann + */ +class TransformationFailureListener implements EventSubscriberInterface +{ + private $translator; + + public function __construct(TranslatorInterface $translator = null) + { + $this->translator = $translator; + } + + public static function getSubscribedEvents() + { + return array( + FormEvents::POST_SUBMIT => array('convertTransformationFailureToFormError', -1024), + ); + } + + public function convertTransformationFailureToFormError(FormEvent $event) + { + $form = $event->getForm(); + + if (null === $form->getTransformationFailure() || !$form->isValid()) { + return; + } + + foreach ($form as $child) { + if (!$child->isSynchronized()) { + return; + } + } + + $clientDataAsString = is_scalar($form->getViewData()) ? (string) $form->getViewData() : \gettype($form->getViewData()); + $messageTemplate = 'The value {{ value }} is not valid.'; + + if (null !== $this->translator) { + $message = $this->translator->trans($messageTemplate, array('{{ value }}' => $clientDataAsString)); + } else { + $message = strtr($messageTemplate, array('{{ value }}' => $clientDataAsString)); + } + + $form->addError(new FormError($message, $messageTemplate, array('{{ value }}' => $clientDataAsString), null, $form->getTransformationFailure())); + } +} diff --git a/src/Symfony/Component/Form/Extension/Core/Type/TransformationFailureExtension.php b/src/Symfony/Component/Form/Extension/Core/Type/TransformationFailureExtension.php new file mode 100644 index 0000000000..98875594d6 --- /dev/null +++ b/src/Symfony/Component/Form/Extension/Core/Type/TransformationFailureExtension.php @@ -0,0 +1,42 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Extension\Core\Type; + +use Symfony\Component\Form\AbstractTypeExtension; +use Symfony\Component\Form\Extension\Core\EventListener\TransformationFailureListener; +use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Translation\TranslatorInterface; + +/** + * @author Christian Flothmann + */ +class TransformationFailureExtension extends AbstractTypeExtension +{ + private $translator; + + public function __construct(TranslatorInterface $translator = null) + { + $this->translator = $translator; + } + + public function buildForm(FormBuilderInterface $builder, array $options) + { + if (!isset($options['invalid_message']) && !isset($options['invalid_message_parameters'])) { + $builder->addEventSubscriber(new TransformationFailureListener($this->translator)); + } + } + + public function getExtendedType() + { + return 'Symfony\Component\Form\Extension\Core\Type\FormType'; + } +} diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/CoreExtensionTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/CoreExtensionTest.php new file mode 100644 index 0000000000..ff85149e21 --- /dev/null +++ b/src/Symfony/Component/Form/Tests/Extension/Core/CoreExtensionTest.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Tests\Extension\Core; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Form\Extension\Core\CoreExtension; +use Symfony\Component\Form\FormFactoryBuilder; + +class CoreExtensionTest extends TestCase +{ + public function testTransformationFailuresAreConvertedIntoFormErrors() + { + $formFactoryBuilder = new FormFactoryBuilder(); + $formFactory = $formFactoryBuilder->addExtension(new CoreExtension()) + ->getFormFactory(); + + $form = $formFactory->createBuilder() + ->add('foo', 'Symfony\Component\Form\Extension\Core\Type\DateType') + ->getForm(); + $form->submit('foo'); + + $this->assertFalse($form->isValid()); + } +}