0
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 3 years have passed since last update.

vue3でバリデーションする

Posted at

vue3でバリデーションについて調べたのでメモ
参考(https://www.npmjs.com/package/@vuelidate/core)

環境

vue3
typescript

vuelidateを使う

現時点でアルファ版ぽい
自分の環境ではバージョンの互換性がなかったので--legacy-peer-depsで強制的にインスコ

npm install @vuelidate/core @vuelidate/validators --save --legacy-peer-deps
main.ts
import { createApp } from 'vue'
import App from './App.vue'
import { VuelidatePlugin } from '@vuelidate/core'
 
const app = createApp(App)
app.use(VuelidatePlugin)
app.mount('#app')
App.vue
<template>
  <div>
    <input v-model="emailAddress" label="メールアドレス" @blur="$v.emailAddress.$touch()" >
    <p v-for="(error, index) of $v.$errors" :key="index">
            {{ error }}
    </p>
  </div>

</template>

<script lang="ts">
import { ref } from 'vue'
import { useVuelidate } from '@vuelidate/core'
import { email, required } from '@vuelidate/validators'

export default {
  setup () {
    const name = ref('')
    const emailAddress = ref('')
    const rules = {
      name: { required },
      emailAddress: { required, email }
    }
 
    const $v = useVuelidate(rules, { name, emailAddress })
 
    return { name, emailAddress, $v }
  }
}
</script>

こんな感じです

0
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
0
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?