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

Event listener for "invalid" is not working #8647

Closed
petr001 opened this issue Aug 13, 2018 · 6 comments
Closed

Event listener for "invalid" is not working #8647

petr001 opened this issue Aug 13, 2018 · 6 comments

Comments

@petr001
Copy link

petr001 commented Aug 13, 2018

Version

2.5.17

Reproduction link

https://codesandbox.io/s/2p6zqqo75j

Steps to reproduce

When form is in invalid state no event handler is called

What is expected?

v-on:invalid should run the handler function

What is actually happening?

nothing

@LinusBorg
Copy link
Member

LinusBorg commented Aug 13, 2018

Not Vue-related,

That event is fired in the input element, not the form element, and it doesn't bubble.

So this works as expected:

<template>
  <form ref="form" @submit.prevent="handleSubmit">
    <input @invalid="handleInvalid" required>
    <button type="submit">Submit</button>

    <p>{{ msg }}</p>
  </form>
</template>

<script>
export default {
  name: "HelloWorld",
  data: () => ({
    msg: 'Not validated yet'
  }),
  methods: {
    handleSubmit() {
      this.msg = 'submit event fired';
    },
    handleInvalid() {
      this.msg = 'invalid event fired';
    }
  }
};
</script>

@petr001
Copy link
Author

petr001 commented Aug 13, 2018

Not true. If you manually addEventListener for invalid on form element it works correctly!

@petr001
Copy link
Author

petr001 commented Aug 13, 2018

This is link for vanilla and working javascript: https://codesandbox.io/s/3vl6rq4jmm

@posva
Copy link
Member

posva commented Aug 13, 2018

You are missing the capture in your vue version: @invalid.capture.prevent="handleInvalid"

@LinusBorg
Copy link
Member

Yeah, posva beat me to it. Only works with capture since the event doesn't bubble.

Should have thought of that solution first.

@petr001
Copy link
Author

petr001 commented Aug 13, 2018

Perfect, working as expected, thank you all.

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

3 participants