Skip to content
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

[Form] bool2string strict comparison #18005

Closed
wants to merge 1 commit into from
Closed

[Form] bool2string strict comparison #18005

wants to merge 1 commit into from

Conversation

phramz
Copy link
Contributor

@phramz phramz commented Mar 3, 2016

Q A
Bug fix? no
New feature? yes
BC breaks? yes
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

The current behaviour of the BooleanToStringTransformer perfectly fits the need of the CheckBoxTypeand how browsers handle form submissions of checkbox-values.
The downside is that any value e.g. "0" will transform to true even if the trueValueis set to "1". Due to this it may cause trouble when you want to handle form-submission by Javascript. For a JS-Dev is may not be 100% clear that "0" will transform to truein the backend.
This PR makes it possible to uncheck values without omitting the form-field in the post-data by submitting "0" or "" as value.

*This PR is a follow up of #15054. Head there to see the full discussion *

@@ -80,6 +94,23 @@ public function reverseTransform($value)
throw new TransformationFailedException('Expected a string.');
}

return true;
if (in_array($value, array_merge($this->defaultTrueValues, array($this->trueValue)), true)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of the expensive expensive array_merge(), could you rearrange this to:

if ($this->trueValue === $value || in_array($value, $this->defaultValues, true)) {

@xabbuh
Copy link
Member

xabbuh commented Mar 7, 2016

This new behaviour looks wrong to me as it complicates things. Why do you send values via JavaScript at all when a checkbox is not checked? If I am not mistaken, this is not how checkboxes are supposed to behave.

@phramz
Copy link
Contributor Author

phramz commented Mar 7, 2016

The point is that you may deal with a frontend model where your checkbox(es) may be bound to a model that reflects those value as "boolean" (eg checkbox:check ["true"] checkbox:not-check ["false"]). The latter would cause the bool2string transformer to evaluate "0", "" or "false" to true which looks wrong to me ... and to many other frontend devs that use the form-component beyond the plain old browser form-post submissions.

You're totally right to say that's how checkboxes supposed to behave but that's actually not the intention of this PR (as i stated in the PRs decription) and will remain as is but eases the pain of the transforming "boolean" values (in the sense of it's real meaning "boolean") where false is false and true is true.

sorry @xabbuh but i'm kind of fed up with this PR cuz I'm waiting since 06/15 to get qualified feedback on this, rebase, push, rebase and push and starting all over again.

@webmozart please make a decision on that now or never ... i'm out

@webmozart
Copy link
Contributor

@phramz Sorry for upsetting you. We need to take care only to merge what's really necessary in order to not bloat the code base.

I have one more question: In the example you gave, do you send the form via CGI (regular form submission) or as part of a JSON object? i.e., could the values 'true' and 'false' also be sent as actual booleans true and false?

@phramz
Copy link
Contributor Author

phramz commented Mar 8, 2016

Ideally both 'true' and 'false' as well as true and false would be possible ... that's the current behaviour of the latest commit.

Don't get me wrong, I really appreciate your and the work of all symfony members and contributors and totally understand that there is always lack of time and also cumbersome stuff to care about.

What makes me unhappy about how that PR evolved is not wether it'll be merged or not nor that it took such a long time but that I think it would have been less frustrating to discuss those pros and cons before you asked me to write more tests and refactor error-messages and such. I'm totally fine with any decision about if it's a "necessary" or even useful feature or not.

@webmozart
Copy link
Contributor

@phramz I understand that both are possible now. What I'm asking is: Can your JavaScript be changed to achieve the desired behavior without merging this PR? Is this a must-have or a nice-to-have for you?

I'm very sorry for your bad experience with working on this PR. I totally understand your frustration. It's often easier for us to spot "obvious" issues such as missing tests or error messages than to judge whether a change actually makes sense. We try to do that, but sometimes that only gets clear after a certain amount of discussion.

@phramz
Copy link
Contributor Author

phramz commented Mar 26, 2016

No worries ... I just put it in a dedicated bool formtype. Thanks for your time.

@phramz phramz closed this Mar 26, 2016
symfony-splitter pushed a commit to symfony/form that referenced this pull request Jan 17, 2018
…oString transformer (leberknecht)

This PR was squashed before being merged into the 4.1-dev branch (closes #25741).

Discussion
----------

[Form] issue-13589: adding custom false-values to BooleanToString transformer

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #13589
| License       | MIT
| Doc PR        | symfony/symfony-docs#9031

As suggested in #13589 , this PR adds the option to specify custom false-values, so we can use the CheckBoxType to handle strings '1' / '0' and eval them to true / false. While HTTP defines that checkbox types are either submitted=true or not-submitted=false, no matter of what value was submitted, it would be nice to have this option.
Also refs (which read like "the basic idea of the feature was accepted, PR almost done, then something-happend so PR wasnt merged"..?)

symfony/symfony#15054
symfony/symfony#18005

Commits
-------

a3e5ac496f [Form] issue-13589: adding custom false-values to BooleanToString transformer
fabpot added a commit that referenced this pull request Jan 17, 2018
…oString transformer (leberknecht)

This PR was squashed before being merged into the 4.1-dev branch (closes #25741).

Discussion
----------

[Form] issue-13589: adding custom false-values to BooleanToString transformer

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #13589
| License       | MIT
| Doc PR        | symfony/symfony-docs#9031

As suggested in #13589 , this PR adds the option to specify custom false-values, so we can use the CheckBoxType to handle strings '1' / '0' and eval them to true / false. While HTTP defines that checkbox types are either submitted=true or not-submitted=false, no matter of what value was submitted, it would be nice to have this option.
Also refs (which read like "the basic idea of the feature was accepted, PR almost done, then something-happend so PR wasnt merged"..?)

#15054
#18005

Commits
-------

a3e5ac4 [Form] issue-13589: adding custom false-values to BooleanToString transformer
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.

None yet

6 participants