Skip to content

Deprecate using Form::isValid() with an unsubmitted form #17644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions UPGRADE-3.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------

Expand Down
19 changes: 19 additions & 0 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------

Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/FormInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/Form/Tests/SimpleFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down