narrow-evening-29329
02/19/2022, 8:29 PMnarrow-evening-29329
02/19/2022, 9:02 PMmysterious-toddler-20573
02/19/2022, 9:02 PMHX-Retarget:body with an HX-Push would workmysterious-toddler-20573
02/19/2022, 9:03 PMnarrow-evening-29329
02/19/2022, 9:40 PMnarrow-evening-29329
02/19/2022, 9:40 PMmysterious-toddler-20573
02/19/2022, 9:54 PMmysterious-toddler-20573
02/19/2022, 9:54 PMnarrow-evening-29329
02/19/2022, 9:56 PMnumerous-application-19425
02/19/2022, 10:19 PMis-invalid if the server rejects a field.
Now I read the section about validation https://htmx.org/docs/#validation but I have no clue how to hook into the native validation and prevent submission.
I read here on the chat that the HTMX way would be to submit the form and deal with the server response. I could live with that as well, but I can't modify the formfield html and I want the "realtime" validation/visual feedback if a field is filled correctly.
How do I approach this? Is it even possible?numerous-application-19425
02/19/2022, 10:23 PMhundreds-camera-24900
02/19/2022, 10:50 PMhundreds-camera-24900
02/19/2022, 10:50 PMhundreds-camera-24900
02/19/2022, 10:50 PMhundreds-camera-24900
02/19/2022, 10:51 PMhundreds-camera-24900
02/19/2022, 10:53 PMacoustic-pager-39127
02/19/2022, 11:36 PMnumerous-application-19425
02/19/2022, 11:37 PMjs
// Example starter JavaScript for disabling form submissions if there are invalid fields
(function () {
'use strict'
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.querySelectorAll('.needs-validation')
// Loop over them and prevent submission
Array.prototype.slice.call(forms)
.forEach(function (form) {
form.addEventListener('submit', function (event) {
if (!form.checkValidity()) {
event.preventDefault()
event.stopPropagation()
}
form.classList.add('was-validated')
}, false)
})
})()
So I'm happy to add some JS, just not directly to the formfields (like in the HTMX/Hyperscript example)
I wonder if this: form.addEventListener('submit' should be something like htmx:beforeRequest ? I can't wrap my head around it yet.
A reason why this is useful (validate clientside first) is, if I upload a 10 MB file, I don't want the form coming back from the server with a field error and then have to upload that file again. Does that make sense?hundreds-camera-24900
02/19/2022, 11:37 PMhundreds-camera-24900
02/19/2022, 11:38 PMhundreds-camera-24900
02/19/2022, 11:39 PMhundreds-camera-24900
02/19/2022, 11:40 PMhundreds-camera-24900
02/19/2022, 11:40 PMhundreds-camera-24900
02/19/2022, 11:40 PMacoustic-pager-39127
02/20/2022, 2:22 AMpowerful-evening-99811
02/20/2022, 3:24 AMon click toggle .highlight on target in some parent elementacoustic-pager-39127
02/20/2022, 3:50 AMquiet-angle-80724
02/20/2022, 8:08 AMbrief-sugar-79424
02/20/2022, 8:59 AM