Skip to content

Commit b23b2f0

Browse files
authored
🐛 Prevents erroring on null vals (#2799)
* 🐛 Prevents erroring on null vals * 🚧 Applies same fix in FormProxy * 🧪 Adds Test for null in FormDataProxy
1 parent 5b550e5 commit b23b2f0

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

src/htmx.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3966,7 +3966,7 @@ var htmx = (function() {
39663966
const formData = new FormData()
39673967
for (const key in obj) {
39683968
if (obj.hasOwnProperty(key)) {
3969-
if (typeof obj[key].forEach === 'function') {
3969+
if (obj[key] && typeof obj[key].forEach === 'function') {
39703970
obj[key].forEach(function(v) { formData.append(key, v) })
39713971
} else if (typeof obj[key] === 'object' && !(obj[key] instanceof Blob)) {
39723972
formData.append(key, JSON.stringify(obj[key]))
@@ -4059,7 +4059,7 @@ var htmx = (function() {
40594059
return false
40604060
}
40614061
target.delete(name)
4062-
if (typeof value.forEach === 'function') {
4062+
if (value && typeof value.forEach === 'function') {
40634063
value.forEach(function(v) { target.append(name, v) })
40644064
} else if (typeof value === 'object' && !(value instanceof Blob)) {
40654065
target.append(name, JSON.stringify(value))

test/attributes/hx-vals.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,15 @@ describe('hx-vals attribute', function() {
297297
}
298298
calledEvent.should.equal(true)
299299
})
300+
it('hx-vals works with null values', function() {
301+
this.server.respondWith('POST', '/vars', function(xhr) {
302+
var params = getParameters(xhr)
303+
params.i1.should.equal('null')
304+
xhr.respond(200, {}, 'Clicked!')
305+
})
306+
var div = make("<div hx-post='/vars' hx-vals='{\"i1\": null }'></div>")
307+
div.click()
308+
this.server.respond()
309+
div.innerHTML.should.equal('Clicked!')
310+
})
300311
})

test/core/parameters.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,14 @@ describe('Core htmx Parameter Handling', function() {
280280
vals.foo.should.equal('bar')
281281
})
282282

283+
it('formdata works with null values', function() {
284+
var form = make('<form hx-post="/test"><input name="foo" value="bar"/></form>')
285+
var vals = htmx._('getInputValues')(form, 'get').values
286+
function updateToNull() { vals.foo = null }
287+
updateToNull.should.not.throw()
288+
vals.foo.should.equal('null')
289+
})
290+
283291
it('order of parameters follows order of input elements', function() {
284292
this.server.respondWith('GET', '/test?foo=bar&bar=foo&foo=bar&foo2=bar2', function(xhr) {
285293
xhr.respond(200, {}, 'Clicked!')

0 commit comments

Comments
 (0)