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

Bug: For set_checkbox, when default is true the checkbox will become checked when redirected back with input. #7814

Closed
WrestlingPotato opened this issue Aug 13, 2023 · 7 comments · Fixed by #7818
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@WrestlingPotato
Copy link

WrestlingPotato commented Aug 13, 2023

PHP Version

8.1

CodeIgniter4 Version

4.3.5

CodeIgniter4 Installation Method

Manual (zip or tar.gz)

Which operating systems have you tested for this bug?

Windows

Which server did you use?

apache

Database

No response

What happened?

<input type="checkbox" name="fruit" value="apple" <?= set_checkbox("fruit", "apple", true) ?>>
The input is part of a user submitted form.
When the view is generated, I want the checkbox to be checked by default. Then the user submits their form, the controller validates the information, then is redirected accordingly.
However, when the user is redirected back with input, 'fruit' is checked, even when the user unchecks it.

I suspect that this is because when 'fruit' is unchecked, 'fruit' is not added to $_POST, then when set_checkbox does not find an old value, which causes it to use the default value, which is checked. I think this because my CI_ENVIRONMENT is development, and I can see that _ci_old_input does not contain 'fruit'.

Steps to Reproduce

have
<input type="checkbox" name="fruit" value="apple" <?= set_checkbox("fruit", "apple", true) ?>>
in the view,

then
return redirect()->back()->withInput();
in the controller, after the user submits the form

Expected Output

Checkbox should be checked by default, then checked/unchecked according to user input when redirected back with input.

Anything else?

No response

@WrestlingPotato WrestlingPotato added the bug Verified issues on the current code behavior or pull requests that will fix them label Aug 13, 2023
@kenjis
Copy link
Member

kenjis commented Aug 14, 2023

Yes, withInput() stores POST values in the session,
and there is no fruit data if you don't check anything.
Then set_checkbox() checks it because the default is true.

This behavior is from CI3, so maybe this is the spec?

@kenjis
Copy link
Member

kenjis commented Aug 14, 2023

@WrestlingPotato If you could fix this issue, please send a PR.
https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/README.md

@kenjis
Copy link
Member

kenjis commented Aug 14, 2023

Ah, CI3 does not have withInput(). So there is no problem!

The workaround is not to use the redirect.

@kenjis
Copy link
Member

kenjis commented Aug 14, 2023

I found a bug in the documentation: #7816

@WrestlingPotato
Copy link
Author

I'm just starting to learn CI4, so I don't know how to fix the issue, and wanted to bring attention to the issue. For now I just gave up on having the checkbox be checked by default.

@neznaika0
Copy link
Contributor

use set_value('fruit', $fruit === 'apple' ? true : false) ? $fruit it _POST value

@kenjis
Copy link
Member

kenjis commented Aug 14, 2023

@WrestlingPotato No problem. I found how to fix. Try #7818

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants