From 2c3a7cc532a8a2fa5c0b81a2b5a6469e05d65024 Mon Sep 17 00:00:00 2001 From: Ener-Getick Date: Mon, 1 Feb 2016 20:26:57 +0100 Subject: [PATCH] Deprecate using Form::isValid() with an unsubmitted form --- UPGRADE-3.2.md | 22 +++++++++++++++++++ UPGRADE-4.0.md | 19 ++++++++++++++++ src/Symfony/Component/Form/Form.php | 2 ++ src/Symfony/Component/Form/FormInterface.php | 2 +- .../Component/Form/Tests/SimpleFormTest.php | 5 ++++- 5 files changed, 48 insertions(+), 2 deletions(-) diff --git a/UPGRADE-3.2.md b/UPGRADE-3.2.md index 211d9f253626..741958a5ff54 100644 --- a/UPGRADE-3.2.md +++ b/UPGRADE-3.2.md @@ -7,6 +7,28 @@ DependencyInjection * Calling `get()` on a `ContainerBuilder` instance before compiling the container is deprecated and will throw an exception in Symfony 4.0. +Form +---- + + * Calling `isValid()` on a `Form` instance before submitting it + is deprecated and will throw an exception in Symfony 4.0. + + Before: + + ```php + if ($form->isValid()) { + // ... + } + ``` + + After: + + ```php + if ($form->isSubmitted() && $form->isValid()) { + // ... + } + ``` + Validator --------- diff --git a/UPGRADE-4.0.md b/UPGRADE-4.0.md index 3687b0b78248..53ecad9e1dfb 100644 --- a/UPGRADE-4.0.md +++ b/UPGRADE-4.0.md @@ -42,6 +42,25 @@ Form * Caching of the loaded `ChoiceListInterface` in the `LazyChoiceList` has been removed, it must be cached in the `ChoiceLoaderInterface` implementation instead. + * Calling `isValid()` on a `Form` instance before submitting it is not supported + anymore and raises an exception. + + Before: + + ```php + if ($form->isValid()) { + // ... + } + ``` + + After: + + ```php + if ($form->isSubmitted() && $form->isValid()) { + // ... + } + ``` + FrameworkBundle --------------- diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index 74ff45b297dc..3d29abe9124d 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -724,6 +724,8 @@ public function isEmpty() public function isValid() { if (!$this->submitted) { + @trigger_error('Call Form::isValid() with an unsubmitted form is deprecated since version 3.2 and will throw an exception in 4.0. Use Form::isSubmitted() before Form::isValid() instead.', E_USER_DEPRECATED); + return false; } diff --git a/src/Symfony/Component/Form/FormInterface.php b/src/Symfony/Component/Form/FormInterface.php index b2131e1da4ec..83fd70a84807 100644 --- a/src/Symfony/Component/Form/FormInterface.php +++ b/src/Symfony/Component/Form/FormInterface.php @@ -192,7 +192,7 @@ public function addError(FormError $error); /** * Returns whether the form and all children are valid. * - * If the form is not submitted, this method always returns false. + * If the form is not submitted, this method always returns false (but will throw an exception in 4.0). * * @return bool */ diff --git a/src/Symfony/Component/Form/Tests/SimpleFormTest.php b/src/Symfony/Component/Form/Tests/SimpleFormTest.php index 36edea5da983..d4d74c28d0f8 100644 --- a/src/Symfony/Component/Form/Tests/SimpleFormTest.php +++ b/src/Symfony/Component/Form/Tests/SimpleFormTest.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Form\Tests; +use Symfony\Bridge\PhpUnit\ErrorAssert; use Symfony\Component\Form\Form; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; @@ -315,7 +316,9 @@ public function testValidIfSubmittedAndDisabled() public function testNotValidIfNotSubmitted() { - $this->assertFalse($this->form->isValid()); + ErrorAssert::assertDeprecationsAreTriggered(array('Call Form::isValid() with an unsubmitted form'), function () { + $this->assertFalse($this->form->isValid()); + }); } public function testNotValidIfErrors()