Skip to content

Commit 7ccfdb4

Browse files
committed
feature symfony#17644 Deprecate using Form::isValid() with an unsubmitted form (Ener-Getick)
This PR was merged into the 3.2-dev branch. Discussion ---------- Deprecate using Form::isValid() with an unsubmitted form | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | symfony#7737 | License | MIT | Doc PR | not yet > I'm calling out for you to decide how to resolve the inconsistency between the Form::isValid() method and the ``valid`` variable available in the template: > > - ``isValid()`` returns ``false`` if a form was not submitted. This way it is possible to write concise controller code: > > ```php > $form = $this->createForm(...); > $form->handleRequest($request); > if ($form->isValid()) { > // only executed if the form is submitted AND valid > } > ``` > > - ``valid`` contains ``true`` if a form was not submitted. This way it is possible to rely on this variable for error styling of a form. > > ```twig > <div{% if not form.vars.valid %} class="error"{% endif %}> > ``` > > We have two alternatives for resolving this problem: > > 1. Leave the inconsistency as is. > 2. Make ``isValid()`` return ``true`` if a form was not submitted (consistent with ``valid``) > 3. Revert to the 2.2 behavior of throwing an exception if ``isValid()`` is called on a non-submitted form (not consistent with ``valid``). > > Both 2. and 3. will require additional code in the controller: > > ```php > $form = $this->createForm(...); > $form->handleRequest($request); > if ($form->isSubmitted() && $form->isValid()) { > // only executed if the form is submitted AND valid > } > ``` > > What do you think? This PR implements the option 3 as it was the most chosen in symfony#7737 Commits ------- 2c3a7cc Deprecate using Form::isValid() with an unsubmitted form
2 parents 22f7ed7 + 2c3a7cc commit 7ccfdb4

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

UPGRADE-3.2.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,28 @@ DependencyInjection
77
* Calling `get()` on a `ContainerBuilder` instance before compiling the
88
container is deprecated and will throw an exception in Symfony 4.0.
99

10+
Form
11+
----
12+
13+
* Calling `isValid()` on a `Form` instance before submitting it
14+
is deprecated and will throw an exception in Symfony 4.0.
15+
16+
Before:
17+
18+
```php
19+
if ($form->isValid()) {
20+
// ...
21+
}
22+
```
23+
24+
After:
25+
26+
```php
27+
if ($form->isSubmitted() && $form->isValid()) {
28+
// ...
29+
}
30+
```
31+
1032
Validator
1133
---------
1234

UPGRADE-4.0.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,25 @@ Form
4242
* Caching of the loaded `ChoiceListInterface` in the `LazyChoiceList` has been removed,
4343
it must be cached in the `ChoiceLoaderInterface` implementation instead.
4444

45+
* Calling `isValid()` on a `Form` instance before submitting it is not supported
46+
anymore and raises an exception.
47+
48+
Before:
49+
50+
```php
51+
if ($form->isValid()) {
52+
// ...
53+
}
54+
```
55+
56+
After:
57+
58+
```php
59+
if ($form->isSubmitted() && $form->isValid()) {
60+
// ...
61+
}
62+
```
63+
4564
FrameworkBundle
4665
---------------
4766

src/Symfony/Component/Form/Form.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,8 @@ public function isEmpty()
724724
public function isValid()
725725
{
726726
if (!$this->submitted) {
727+
@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);
728+
727729
return false;
728730
}
729731

src/Symfony/Component/Form/FormInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public function addError(FormError $error);
192192
/**
193193
* Returns whether the form and all children are valid.
194194
*
195-
* If the form is not submitted, this method always returns false.
195+
* If the form is not submitted, this method always returns false (but will throw an exception in 4.0).
196196
*
197197
* @return bool
198198
*/

src/Symfony/Component/Form/Tests/SimpleFormTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Form\Tests;
1313

14+
use Symfony\Bridge\PhpUnit\ErrorAssert;
1415
use Symfony\Component\Form\Form;
1516
use Symfony\Component\Form\FormEvent;
1617
use Symfony\Component\Form\FormEvents;
@@ -315,7 +316,9 @@ public function testValidIfSubmittedAndDisabled()
315316

316317
public function testNotValidIfNotSubmitted()
317318
{
318-
$this->assertFalse($this->form->isValid());
319+
ErrorAssert::assertDeprecationsAreTriggered(array('Call Form::isValid() with an unsubmitted form'), function () {
320+
$this->assertFalse($this->form->isValid());
321+
});
319322
}
320323

321324
public function testNotValidIfErrors()

0 commit comments

Comments
 (0)