feat: disabling auth submit on empty names

https://htmx.org/attributes/hx-validate/
for 'non form submittions' to trigger validation

custom check, because 'required' marks fields red immediately
and this js does only on attempted submittion,
not sure i want it, but seems better right now.
also - such a good argument for hyperscript
This commit is contained in:
efim 2023-11-05 04:14:36 +00:00
parent 3a6fe28981
commit 83c634c2b9
2 changed files with 65 additions and 8 deletions

View File

@ -612,3 +612,8 @@ video {
font-size: 1.25rem; font-size: 1.25rem;
line-height: 1.75rem; line-height: 1.75rem;
} }
.invalid\:bg-red-700:invalid {
--tw-bg-opacity: 1;
background-color: rgb(185 28 28 / var(--tw-bg-opacity));
}

View File

@ -49,6 +49,7 @@
hx-post="/login/room-name-check" hx-post="/login/room-name-check"
hx-target="#formButton" hx-target="#formButton"
hx-swap="outerHTML" hx-swap="outerHTML"
class="invalid:bg-red-700"
/> />
<input <input
id="roomPassword" id="roomPassword"
@ -66,6 +67,7 @@
name="personalName" name="personalName"
value="" value=""
placeholder="personal name" placeholder="personal name"
class="invalid:bg-red-700"
/> />
<input <input
id="personalPassword" id="personalPassword"
@ -75,17 +77,67 @@
placeholder="personal password" placeholder="personal password"
/> />
</div> </div>
{{ block "formButton" .IsRoomExisting }} {{ block "formButton" .IsRoomExisting }} {{ if not . }}
{{ if not . }} <button
<button id="formButton" class="col-span-full" hx-post="/login/create">Create Room</button> id="formButton"
class="col-span-full"
hx-post="/login/create"
hx-validate="true"
>
Create Room
</button>
{{ else }} {{ else }}
<button id="formButton" class="col-span-full" hx-post="/login/join">Join Room</button> <button
{{ end }} id="formButton"
{{ end }} class="col-span-full"
hx-post="/login/join"
hx-validate="true"
>
Join Room
</button>
{{ end }} {{ end }}
</form> </form>
<script> <script>
window.addEventListener('DOMContentLoaded', (event) => { window.addEventListener("DOMContentLoaded", (event) => {
document.getElementById('loginForm').reset(); document.getElementById("loginForm").reset();
});
const requiredInputFunc = function (event) {
// If the input is empty...
if (this.value.trim() === "") {
// ...set a custom validity message
console.log("empty name");
this.setCustomValidity("Please fill out this field.");
this.checkValidity();
} else {
// If not empty, clear any custom validity message
console.log("other");
this.setCustomValidity("");
}
};
// Find the input element
const personalNameInput = document.getElementById("personalName");
const roomNameInput = document.getElementById("roomName");
// Listen for the 'htmx:validation:validate' event on the input element
personalNameInput.addEventListener(
"htmx:validation:validate",
requiredInputFunc,
);
roomNameInput.addEventListener(
"htmx:validation:validate",
requiredInputFunc,
);
// You can also listen for the 'input' event to clear the validation message as the user types
personalNameInput.addEventListener("input", function (event) {
// Always clear the custom validity message when the user types
this.setCustomValidity("");
});
roomNameInput.addEventListener("input", function (event) {
// Always clear the custom validity message when the user types
this.setCustomValidity("");
}); });
</script> </script>
</section> </section>