bigskysoftware / bigskysoftware/htmx-extensions

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

Open
#35 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
JavaScript
Stars
284
Forks
88
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.