From c3b3c44a61e439eba520c411c022d6a2833bacd5 Mon Sep 17 00:00:00 2001 From: Ed Henighan Date: Mon, 19 Aug 2024 10:59:09 +0100 Subject: [PATCH 1/2] Adding tests to demonstrate https://github.com/bigskysoftware/htmx/issues/2676 --- test/core/ajax.js | 29 +++++++++++++++++++++++++++++ test/core/validation.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/test/core/ajax.js b/test/core/ajax.js index 58603e851..5ed78c925 100644 --- a/test/core/ajax.js +++ b/test/core/ajax.js @@ -586,6 +586,35 @@ describe("Core htmx AJAX Tests", function(){ }); + it('properly handles radio inputs', function() + { + var values; + this.server.respondWith('Post', '/test', function(xhr) { + values = getParameters(xhr) + xhr.respond(204, {}, '') + }); + + var form = make('
' + + '
' + + '' + + '' + + '' + + '' + + '' + + '' + + '
' + + '
'); + + form.click(); + this.server.respond(); + values.should.deep.equal({}); + + byId('rb1').checked = true; + form.click(); + this.server.respond(); + values.should.deep.equal({ r1: 'rb1' }); + }); + it('text nodes dont screw up settling via variable capture', function() { this.server.respondWith("GET", "/test", "
fooo"); diff --git a/test/core/validation.js b/test/core/validation.js index 075e46391..5a2d7bea1 100644 --- a/test/core/validation.js +++ b/test/core/validation.js @@ -108,6 +108,42 @@ describe("Core htmx client side validation tests", function(){ form.textContent.should.equal("Clicked!"); }); + it('Custom validation error prevents request for unticked checkboxes', function() { + this.server.respondWith('POST', '/test', 'Clicked!'); + + var form = make('
' + + 'No Request' + + '' + + '
'); + byId('i1').setCustomValidity('Nope'); + form.textContent.should.equal('No Request'); + form.click(); + this.server.respond(); + form.textContent.should.equal('No Request'); + byId('i1').setCustomValidity(''); + form.click(); + this.server.respond(); + form.textContent.should.equal('Clicked!'); + }); + + it('Custom validation error prevents request for unselected radiogroups', function() { + this.server.respondWith('POST', '/test', 'Clicked!'); + + var form = make('
' + + 'No Request' + + '' + + '
'); + byId('i1').setCustomValidity('Nope'); + form.textContent.should.equal('No Request'); + form.click(); + this.server.respond(); + form.textContent.should.equal('No Request'); + byId('i1').setCustomValidity(''); + form.click(); + this.server.respond(); + form.textContent.should.equal('Clicked!'); + }); + it('hyperscript validation error prevents request', function() { if (IsIE11()) { From 0bc0ed6d4c4d5f0a28cde01d2fda3b950a730f1b Mon Sep 17 00:00:00 2001 From: Ed Henighan Date: Mon, 19 Aug 2024 10:59:36 +0100 Subject: [PATCH 2/2] Adding v1 fix for https://github.com/bigskysoftware/htmx/issues/2676 --- src/htmx.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/htmx.js b/src/htmx.js index 597c9b3b5..ed9f8d4c7 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -2502,12 +2502,19 @@ return (function () { return false; } - function shouldInclude(elt) { - if(elt.name === "" || elt.name == null || elt.disabled || closest(elt, "fieldset[disabled]")) { + function couldInclude(elt) { + if (elt.name === '' || elt.name == null || elt.disabled || closest(elt, 'fieldset[disabled]')) { return false; } // ignore "submitter" types (see jQuery src/serialize.js) - if (elt.type === "button" || elt.type === "submit" || elt.tagName === "image" || elt.tagName === "reset" || elt.tagName === "file" ) { + if (elt.type === 'button' || elt.type === 'submit' || elt.tagName === 'image' || elt.tagName === 'reset' || elt.tagName === 'file') { + return false; + } + return true; + } + + function shouldInclude(elt) { + if (!couldInclude(elt)) { return false; } if (elt.type === "checkbox" || elt.type === "radio" ) { @@ -2516,6 +2523,10 @@ return (function () { return true; } + function shouldValidate(elt) { + return couldInclude(elt); + } + function addValueToValues(name, value, values) { // This is a little ugly because both the current value of the named value in the form // and the new value could be arrays, so we have to handle all four cases :/ @@ -2556,9 +2567,9 @@ return (function () { value = toArray(elt.files); } addValueToValues(name, value, values); - if (validate) { - validateElement(elt, errors); - } + } + if (validate && shouldValidate(elt)) { + validateElement(elt, errors); } if (matches(elt, 'form')) { var inputs = elt.elements;