Skip validation if form submit button has formnovalidate attribute (#826)

awesome, thank you for adding a test too!
This commit is contained in:
Adam Boaler 2022-02-17 13:15:23 +00:00 committed by GitHub
parent 84ef04beeb
commit f302ad6fdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View File

@ -2060,9 +2060,13 @@ return (function () {
var values = {};
var formValues = {};
var errors = [];
var internalData = getInternalData(elt);
// only validate when form is directly submitted and novalidate is not set
// only validate when form is directly submitted and novalidate or formnovalidate are not set
var validate = matches(elt, 'form') && elt.noValidate !== true;
if (internalData.lastButtonClicked) {
validate = validate && internalData.lastButtonClicked.formNoValidate !== true;
}
// for a non-GET include the closest form
if (verb !== 'get') {
@ -2073,7 +2077,6 @@ return (function () {
processInputValue(processed, values, errors, elt, validate);
// if a button or submit was clicked last, include its value
var internalData = getInternalData(elt);
if (internalData.lastButtonClicked) {
var name = getRawAttribute(internalData.lastButtonClicked,"name");
if (name) {

View File

@ -55,6 +55,21 @@ describe("Core htmx client side validation tests", function(){
form.textContent.should.equal("Clicked!");
});
it('Formnovalidate skips form validation', function()
{
this.server.respondWith("POST", "/test", "Clicked!");
var form = make('<form hx-post="/test">' +
'No Request' +
'<input id="i1" name="i1" required>' +
'<button id="button" type="submit" formnovalidate></button>' +
'</form>');
form.textContent.should.equal("No Request");
byId("button").click();
this.server.respond();
form.textContent.should.equal("Clicked!");
});
it('HTML5 pattern validation error prevents request', function()
{
this.server.respondWith("POST", "/test", "Clicked!");