-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[3.0] [Form] Inconsistency between isValid() and the "valid" template variable #7737
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
Comments
What about option 4: |
@drola Changing |
I would go for n°3 as an unsubmitted form is neither valid or invalid. Despite the additional code. |
In that case I vote for option 3 too. I believe it's the best one, because the developer learns about it in a pretty explicit way. Option 2 would be bad because it changes behaviour without throwing any errors - a pretty hard to find bug in an application. Especially because one does not expect to get |
You could consider a "valid form" to be simply one without errors. Option 2 satisfies this. And the |
Yeah, it really depends whether you define "valid" as errorless or one containing data ready for further processing... Which was the initial source of inconsistency. |
👍 3 |
|
@mvrhov then I would prefer |
either way is fine by me. :) Whatever is shorter... |
My first instinct would be to not consider an unsubmitted form to be valid. An unsubmitted form is in effect a form where all values are unset, which will very rarely be considered valid. And even if that were allowed for a form, the user submitting the form wouldn't have explicitly declared that the intention was for all fields to be blank. If anything, isValid/valid should return null, but that wouldn't be very useful in this context. I also believe |
@bschussek re
The fact that you could consider a "valid form" form to mean two different things says to me that the term valid form shouldn't be used. If something can be misinterpreted, it will be misinterpreted. |
@gunnarlium But from the view perspective the form can be valid if not submitted which just means no errors to display. @bschussek How about a rename in the view and combining isValid with isSubmitted? Of course keep aliases to avoid bc breaks. |
@peterrehm This supports my argument that the term "valid" isn't the right one in this context. What you want is to do something if the form has errors: |
|
That's true, but it's hard to find a good alternative. Documentation helps to clarify such uncertainties. |
@bschussek If it is not possible to combine both I would consider leaving the inconsistency and documenting it. |
What do you think now? Because it is referenced to 3.0 a BC break would be allowed. |
I prefer the option 2, seems to be the more intuitive. |
I feel the same. Though we then again would have another inconsistency: If the form was not submitted before, you would not be able to call |
please see #17644 |
…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
Uh oh!
There was an error while loading. Please reload this page.
I'm calling out for you to decide how to resolve the inconsistency between the
Form::isValid()
method and thevalid
variable available in the template:isValid()
returnsfalse
if a form was not submitted. This way it is possible to write concise controller code:valid
containstrue
if a form was not submitted. This way it is possible to rely on this variable for error styling of a form.We have two alternatives for resolving this problem:
isValid()
returntrue
if a form was not submitted (consistent withvalid
)isValid()
is called on a non-submitted form (not consistent withvalid
).Both 2. and 3. will require additional code in the controller:
What do you think?
The text was updated successfully, but these errors were encountered: