Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form [submit] skips validation with JavaScript event #377

Open
cfoegen-gvboces opened this issue Feb 5, 2025 · 0 comments
Open

Form [submit] skips validation with JavaScript event #377

cfoegen-gvboces opened this issue Feb 5, 2025 · 0 comments

Comments

@cfoegen-gvboces
Copy link

Hello!

I ran across the following odd behavior while building a form with mdui v2.1.3.
When using javascript to add an eventListener to the form, the mdui form appears to ignore validation and submit anyways.
If either is empty, the mdui-text-field will accurately return false via .checkValidity(), but form and the md-button will still return true.

I was able to reproduce this using the following quick example:

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="https://unpkg.com/mdui@2/mdui.css">
    <script src="https://unpkg.com/mdui@2/mdui.global.js"></script>
  </head>
  <body>

    <form id="mdui-form">
      <mdui-text-field name="username" type="text" required></mdui-text-field>
      <mdui-text-field name="password" type="password" required minlength="15"></mdui-text-field>
      <mdui-button type="submit">Login</mdui-button>
    </form>

    <hr/>

    <form id="raw-form">
      <input name="user" type="text" required />
      <br/>
      <input name="passwd" type="password" required minlength="15" />
      <br/>
      <button type="submit">Login</button>
    </form>

    <script>
      const mduiForm = document.getElementById("mdui-form")
      const rawForm = document.getElementById("raw-form")

      function SubmitForm(event) {
	event.preventDefault()
        alert("Form submit!")
      }

      mduiForm.addEventListener("submit", SubmitForm)
      rawForm.addEventListener("submit", SubmitForm)
    </script>

  </body>
</html>

I ended up finding mdui after learning that Material Web Components was in maintenance mode, and I definitely appreciate all the hard work you've put into this!
It has definitely made the front-end part of my web development significantly more pleasant.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant