Fix handling of repeated empty inputs (#1144)

Native form handling submits repeated empty inputs as an array with an
empty value for each input, but htmx would only submit the first
non-empty input and any following inputs. Fix the value collection code
so that it correctly distinguishes between empty values and new inputs.
This commit is contained in:
Billy Keyes 2022-12-02 11:35:59 -05:00 committed by GitHub
parent 90db47fbea
commit 8520f6f374
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 1 deletions

View File

@ -2142,7 +2142,7 @@ return (function () {
// and the new value could be arrays, so we have to handle all four cases :/
if (name != null && value != null) {
var current = values[name];
if(current) {
if (current !== undefined) {
if (Array.isArray(current)) {
if (Array.isArray(value)) {
values[name] = current.concat(value);

View File

@ -78,6 +78,12 @@ describe("Core htmx Parameter Handling", function() {
vals['do'].should.deep.equal(['rey1', 'rey2']);
})
it('Double empty values are included as array in correct order', function () {
var form = make('<form><input id="i1" name="do" value=""/><input id="i2" name="do" value="rey"/><input id="i3" name="do" value=""/></form>');
var vals = htmx._('getInputValues')(byId("i3")).values;
vals['do'].should.deep.equal(['', 'rey', '']);
})
it('hx-include works with form', function () {
var form = make('<form id="f1"><input id="i1" name="foo" value="bar"/><input id="i2" name="do" value="rey"/><input id="i2" name="do" value="rey"/></form>');
var div = make('<div hx-include="#f1"></div>');