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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[馃悶]Form gets checkbox data #6211

Closed
rendaoer opened this issue May 3, 2024 · 9 comments
Closed

[馃悶]Form gets checkbox data #6211

rendaoer opened this issue May 3, 2024 · 9 comments
Assignees
Labels
STATUS-1: needs triage New issue which needs to be triaged TYPE: bug Something isn't working

Comments

@rendaoer
Copy link

rendaoer commented May 3, 2024

Which component is affected?

Qwik City (routing)

Describe the bug

The form uses action to submit multiple CheckBox data and cannot obtain the data array. Only a single CheckBox data can be obtained

Reproduction

https://github.com/rendaoer/qwik-app

Steps to reproduce

pnpm run dev

System Info

System:
    OS: macOS 14.4.1
    CPU: (8) arm64 Apple M1
    Memory: 82.20 MB / 8.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.12.2 - /opt/homebrew/opt/node@20/bin/node
    Yarn: 1.22.22 - /opt/homebrew/opt/node@20/bin/yarn
    npm: 10.5.0 - /opt/homebrew/opt/node@20/bin/npm
    pnpm: 9.0.6 - /opt/homebrew/opt/node@20/bin/pnpm
  Browsers:
    Safari: 17.4.1
  npmPackages:
    @builder.io/qwik: ^1.5.3 => 1.5.3
    @builder.io/qwik-city: ^1.5.3 => 1.5.3
    undici: ^6.15.0 => 6.15.0
    vite: ^5.2.10 => 5.2.10

Additional Information

No response

@rendaoer rendaoer added STATUS-1: needs triage New issue which needs to be triaged TYPE: bug Something isn't working labels May 3, 2024
@yousefmarey12
Copy link

@rendaoer hi, I would be willing to work on it. Can you assign me as an assignee?

@thejackshelton
Copy link
Member

@yousefmarey12 here ya go 馃槃

@yousefmarey12
Copy link

@thejackshelton I am done, do I PR in the reproduction repo or where exactly do I submit the PR?

@thejackshelton
Copy link
Member

thejackshelton commented May 13, 2024

@thejackshelton I am done, do I PR in the reproduction repo or where exactly do I submit the PR?

https://github.com/QwikDev/qwik/blob/main/CONTRIBUTING.md

The contributing file here goes in-depth on how to make a PR to the Qwik repo.

Did you find the solution in the application? Or did you need to change something in the Qwik repo?

cc @yousefmarey12

@rendaoer
Copy link
Author

thank you all

@yousefmarey12
Copy link

@thejackshelton I found the solution in the application.

@yousefmarey12
Copy link

@rendaoer

The Complex Forms Docs says that you get an object and not an array as a result of the form submission.

In your case, having a form name of just "worlds" would just return an object that has "worlds" as a property and ONE value since it's a key-value pair. The solution is to make the form like this

<label for="w1">A</label>
      <input id="w1" name="worlds.A" type="checkbox" value="A" />
      <label for="w2">B</label>
      <input id="w2" name="worlds.B" type="checkbox" value="B" />
      <label for="w3">C</label>
      <input id="w3" name="worlds.C" type="checkbox" value="C" />
      <button type="submit">Submit</button>

This will submit only a worlds object but its value will be the ones that you checked. The ones that you checked will of course, contain the actual value.

Now if you really want an array then you can do

export const useSubmitForm = routeAction$((data) => {
  console.log(inspect(Object.values(data.worlds), { depth: null })); // consoles the array
  zod$({ worlds: z.array(z.string()) });
});

This makes an array of the values of data.worlds and that's the array of all checked boxes for you.

Did I solve your problem?

p.s inspect is just a built-in function in node just to help me read the values.

@rendaoer
Copy link
Author

@yousefmarey12
However, back-end frameworks usually provide data list acquisition or automatic type conversion. Normal form submission also has multiple checkbox data, so I think it is not intuitive.

@thejackshelton
Copy link
Member

thejackshelton commented May 15, 2024

Hey @rendaoer!

If you don't find the API intuitive, you could make a feature request in this repo with some context, the preferred API and the reasoning behind it.

PR's are always welcome too 馃挭

I'm going to close this issue, as it is not a bug in core or qwik city, and @yousefmarey12 has taken the time to find a solution for ya that works (and is documented)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
STATUS-1: needs triage New issue which needs to be triaged TYPE: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants