Vue: Event listener for "invalid" is not working

Created on 13 Aug 2018  路  6Comments  路  Source: vuejs/vue

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

Most helpful comment

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

All 6 comments

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>

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

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

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

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

Should have thought of that solution first.

Perfect, working as expected, thank you all.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

julianxhokaxhiu picture julianxhokaxhiu  路  3Comments

wufeng87 picture wufeng87  路  3Comments

loki0609 picture loki0609  路  3Comments

aviggngyv picture aviggngyv  路  3Comments

6pm picture 6pm  路  3Comments