8000 Deprecate using Form::isValid() with an unsubmitted form · symfony/symfony@5a639dd · GitHub
[go: up one dir, main page]

Skip to content

Commit 5a639dd

Browse files
committed
Deprecate using Form::isValid() with an unsubmitted form
1 parent 22f7ed7 commit 5a639dd

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 unsubmited 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 unsubmited form'), function () {
320+
$this->assertFalse($this->form->isValid());
321+
});
319322
}
320323

321324
public function testNotValidIfErrors()

0 commit comments

Comments
 (0)
0