forked from symfony/symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
invalidate forms on transformation failures
- Loading branch information
Showing
5 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/Symfony/Component/Form/Extension/Core/EventListener/TransformationFailureListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?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\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 <christian.flothmann@sensiolabs.de> | ||
*/ | ||
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; | ||
} | ||
|
||
$childrenSynchronized = true; | ||
|
||
foreach ($form as $child) { | ||
if (!$child->isSynchronized()) { | ||
$childrenSynchronized = false; | ||
break; | ||
} | ||
} | ||
|
||
if ($childrenSynchronized) { | ||
$clientDataAsString = is_scalar($form->getViewData()) ? (string) $form->getViewData() : \gettype($form->getViewData()); | ||
$message = 'This value is not valid.'; | ||
|
||
if (null !== $this->translator) { | ||
$message = $this->translator->trans($message, array('{{ value }}' => $clientDataAsString)); | ||
} | ||
|
||
$form->addError(new FormError($message, 'This value is not valid.', array('{{ value }}' => $clientDataAsString), null, $form->getTransformationFailure())); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Symfony/Component/Form/Extension/Core/Type/TransformationFailureExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?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\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 <christian.flothmann@sensiolabs.de> | ||
*/ | ||
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'; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Symfony/Component/Form/Tests/Extension/Core/CoreExtensionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?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\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()); | ||
} | ||
} |