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

WS json encoding of form with array data not correctly encoding as array with single item #35

Open
PhilipBatten opened this issue Jun 28, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@PhilipBatten
Copy link

I am using the WS extension to post a dynamic form where items can be added and removed. The key of the input is something like items[] to allow the multiple items to be submitted as an array. When there are 2 or more inputs with the name in the form the message posted is correct:

{"name":"multiple array item","items[]":["a","b"],"HEADERS":{"HX-Request":"true","HX-Trigger":null,"HX-Trigger-Name":null,"HX-Target":null,"HX-Current-URL":"http://localhost:8080/"}}

But when only a single item exists this is the message:
{"name":"single array item","items[]":"a","HEADERS":{"HX-Request":"true","HX-Trigger":null,"HX-Trigger-Name":null,"HX-Target":null,"HX-Current-URL":"http://localhost:8080/"}}

As you can see the items[] value is only a string not an array for the single item. When posting the form in the traditional way the post data is always an array.

I think this is a result of line 342 (in the main branch) on ws.js:
var rawParameters = Object.assign({}, results.values)

An additional check needs to be added after this to convert any string values to arrays where the key ends in []

I have a test repository here where I implemented a naive fix: https://github.com/PhilipBatten/htmx-ws-bug

I will attempt an PR to fix this issue if there is no objection.

Thanks.

@rsansores
Copy link

This is affecting json-enc extension too. Haven't saw the code, but maybe WS is using json-enc internally?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants