Replies: 2 comments
-
|
How would this work when you want to modify the input fields and add a class/attribute to, for example, give them a red outline? This isn't a criticism but a genuine question. I suppose you could listen to And on that note, if you're using HTTPS, what are the risks involved with sending the password over the wire? You've got to send it at least once—to the server—so does sending it back make any difference? |
Beta Was this translation helpful? Give feedback.
-
|
I had a similar issue with where I was comparing if the 2 pass-word fields had the same value in the CLIENT side. What I did, was using html built in form validation and then prevented the form request by listening in Javascript (JS) for the "htmx:beforeSend" event, that bubbles when the element triggers the request; which I can prevent with the "preventDefault()" JS after my custom validation. <script>
const formElement= document.querySelector('form');
formElement.addEventListener('htmx:beforeSend', (event) => {
if ( formElement.passwordField.value !== formElement.confirmField.value ) {
event.preventDefault();
//trigger a warning
}
});
</script>This way the form is not submitted if it doesn't meet the criterion, saving a few request to the server. I know I'm a year late but this may help beginners like me some headaches. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In the examples on form validation I have seen that this approach is followed:
This method has two disadvantages.
Solution
It would be possible, if the validation of the form is not successful, to send the client only the errors to be displayed in the form.
It would be possible to do this by using the hx-swap-oob = "true" attribute for each error.
Example of the form:
Response from the server
When these errors are sent back to the client, they are added to the incorrect fields.
The form, and all the inpunt fields are not deleted and recreated.
Beta Was this translation helpful? Give feedback.
All reactions