diff --git a/src/htmx.js b/src/htmx.js index ef03c96a..7bdfc0ee 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -1568,7 +1568,11 @@ return (function () { function getInputValues(elt, verb) { var processed = []; - var values = {}; + var values = { + form: {}, + element: {}, + includes: {}, + }; var errors = []; // only validate when form is directly submitted and novalidate is not set @@ -1576,23 +1580,25 @@ return (function () { // for a non-GET include the closest form if (verb !== 'get') { - processInputValue(processed, values, errors, closest(elt, 'form'), validate); + processInputValue(processed, values.form, errors, closest(elt, 'form'), validate); } // include the element itself - processInputValue(processed, values, errors, elt, validate); + processInputValue(processed, values.element, errors, elt, validate); // include any explicit includes var includes = getClosestAttributeValue(elt, "hx-include"); if (includes) { var nodes = getDocument().querySelectorAll(includes); forEach(nodes, function(node) { - processInputValue(processed, values, errors, node, validate); + processInputValue(processed, values.includes, errors, node, validate); }); } + var mergedValues = mergeObjects(values.includes, values.element); + mergedValues = mergeObjects(mergedValues, values.form); - return {errors:errors, values:values}; + return {errors:errors, values:mergedValues}; } function appendParam(returnStr, name, realValue) { diff --git a/test/attributes/hx-include.js b/test/attributes/hx-include.js index 1d00f25d..b7c8a826 100644 --- a/test/attributes/hx-include.js +++ b/test/attributes/hx-include.js @@ -34,6 +34,22 @@ describe("hx-include attribute", function() { div.innerHTML.should.equal("Clicked!"); }); + it('non-GET includes closest form and overrides values included that exist outside the form', function () { + this.server.respondWith("POST", "/include", function (xhr) { + var params = getParameters(xhr); + params['i1'].should.equal("test"); + xhr.respond(200, {}, "Clicked!") + }); + var div = make('
' + + '' + + '
' + + '') + var input = byId("d1") + input.click(); + this.server.respond(); + div.innerHTML.should.equal("Clicked!"); + }); + it('GET does not include closest form by default', function () { this.server.respondWith("GET", "/include", function (xhr) { var params = getParameters(xhr); @@ -150,4 +166,4 @@ describe("hx-include attribute", function() { }); -}); \ No newline at end of file +});