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

Conversation

GuilhemN
Copy link
Contributor

@GuilhemN GuilhemN commented Feb 1, 2016

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? yes
Tests pass? yes
Fixed tickets #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:
$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.
<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:

$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 #7737

@stof
Copy link
Member

stof commented Feb 1, 2016

We cannot do 2. as it would be a BC break (and would be impossible to make it a soft-migration as there would be no sane way to decide what we should return in this method: the old or new return value)

@GuilhemN
Copy link
Contributor Author

GuilhemN commented Feb 1, 2016

@stof this is a quote of #7737 (it was removed when I submitted my message).

I guess it would be better to continue the conversation in #7737 if we still don't know which option to choose.

@fabpot
Copy link
Member

fabpot commented Jun 16, 2016

👍

@fabpot
Copy link
Member

fabpot commented Jun 16, 2016

ping @symfony/deciders

@fabpot
Copy link
Member

fabpot commented Jun 16, 2016

@Ener-Getick Can you fix the tests?

@fabpot
Copy link
Member

fabpot commented Jun 16, 2016

Also, can you update the CHANGELOG/UPGRADE files?

@GuilhemN
Copy link
Contributor Author

@fabpot i won't be able to do it during the next weeks but of course i'll do it as soon as i can ☺

@@ -724,6 +724,8 @@ public function isEmpty()
public function isValid()
{
if (!$this->submitted) {
@trigger_error('Call Form::isValid() with an unsubmited form is deprecated since version 3.1 and will throw an exception in 4.0. Use Form::isSubmitted() before Form::isValid() instead', E_USER_DEPRECATED);
Copy link
Contributor

@dosten dosten Jun 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... since 3.2 ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, thanks !

@GuilhemN GuilhemN force-pushed the FORM_ISVALID branch 2 times, most recently from 5a639dd to 7c3164f Compare June 17, 2016 15:55
@GuilhemN
Copy link
Contributor Author

GuilhemN commented Jun 17, 2016

After all I found some time to update this PR.
So the tests pass now and I updated the UPGRADE files.

@wouterj
Copy link
Member

wouterj commented Jun 17, 2016

Looks good

Status: reviewed

(oh, btw, the color of "feature" and "status: reviewed" labels look very ugly next to eachother)

@fabpot
Copy link
Member

fabpot commented Jun 17, 2016

Thank you @Ener-Getick.

@fabpot fabpot merged commit 2c3a7cc into symfony:master Jun 17, 2016
fabpot added a commit that referenced this pull request Jun 17, 2016
…rm (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 | #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 #7737

Commits
-------

2c3a7cc Deprecate using Form::isValid() with an unsubmitted form
@GuilhemN GuilhemN deleted the FORM_ISVALID branch June 17, 2016 17:35
craue added a commit to craue/CraueFormFlowBundle that referenced this pull request Sep 7, 2016
@fabpot fabpot mentioned this pull request Oct 27, 2016
@lyrixx
Copy link
Member

lyrixx commented Dec 12, 2016

Hello.

I know it's a bit late for this one, But I would like to vote for the 2/ options.
It makes code longer to write and, even if I understand your arguments here, I'm not sure it worth this change.

Or We could implement something different: changing the value of valid is the template...

@fabpot
Copy link
Member

fabpot commented Dec 13, 2016

@lyrixx I would create a PR or an issue instead of commenting on an already merged PR.

@xabbuh
Copy link
Member

xabbuh commented Dec 13, 2016

@lyrixx Though note that option 2 would be a BC break as pointed out by @stof (see #17644 (comment)).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants