2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Vue.jsでHTMLのバリデーションが効かなくて詰まった時の話

Posted at

inputタグに使用できる入力必須の機能

入力してなかったら弾かれるという基礎的なバリデーションの機能をつけようとして、よく使用するhtmlの機能のrequired="true"で処理しようとした。が、バリデーションが効かず入力してなくても次のページへ遷移してしまう。。。

HTML、CSSに関しては個人アプリやスクールで利用していたため、requiredが効かない原因がVue.jsの何らかの機能のせいなのだろうということは何となくわかったのですが、知識がなくてどこでバリデーションが無効になっているのかがわからず...

原因

submitボタンでの処理が原因でした。

//template部分
@on-click="$emit('next'); submit()"

この一行。

//js部分
  methods: {
    submit() {
      this.$emit("update", {
        eventId: this.$route.params.id,
        Name: this.Name,
        mailAddress: this.mailAddress,
      });
    }
  }

クリックした時に関数のsubmitが発火。$emit('next')で次に遷移している。
この$emit('next')で、バリデーションに引っかかってようがいまいが、データを持って次に遷移してしまっている。
なので、バリデーションをかけるのはsubmitの関数の中。

  methods: {
    submit() {
      if(this.eventId && this.eventId && this.eventId){
        this.$emit("update", {
          eventId: this.$route.params.id,
          Name: this.Name,
          mailAddress: this.mailAddress,
        });
        this.$emit('next');
      }
    }
  }

こうすることで、入力していないとボタンが押せなくなりました。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?