105. Validation Triggers

npm create vue@latest


  1. ๊ฒ€์ฆ ํŠธ๋ฆฌ๊ฑฐ์— ๋Œ€ํ•œ ์„ค๋ช…

    • validate๋Š” ๋‹ค์–‘ํ•œ ์ด๋ฒคํŠธ์—์„œ ์ž…๋ ฅ์„ ๊ฒ€์ฆํ•˜๋„๋ก ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ
    • ๊ธฐ๋ณธ์ ์œผ๋กœ ๋„ค ๊ฐ€์ง€ ๊ฒฝ์šฐ์— ๊ฒ€์ฆ์ด ์ˆ˜ํ–‰๋จ:
      1. change ์ด๋ฒคํŠธ
      2. JavaScript๋ฅผ ํ†ตํ•œ ๋‚ด๋ถ€ ๊ฐ’ ๋ณ€๊ฒฝ
      3. blur ์ด๋ฒคํŠธ
      4. ํผ ์ œ์ถœ ์‹œ
  2. ๊ธฐ๋ณธ ๊ฒ€์ฆ ๋™์ž‘

    • change ์ด๋ฒคํŠธ: ์š”์†Œ ๊ฐ’์˜ ๋ณ€๊ฒฝ์‹œ ๋ฐœ์ƒ
    • JavaScript๋ฅผ ํ†ตํ•œ ๊ฐ’ ๋ณ€๊ฒฝ: v-model ์ง€์‹œ๋ฌธ์„ ์ด์šฉํ•œ ๊ฐ’ ๋ณ€๊ฒฝ ์‹œ ๊ฒ€์ฆ ๋ฐœ์ƒ
    • blur ์ด๋ฒคํŠธ: ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅ ํ•„๋“œ๋ฅผ ํ„ฐ์น˜ํ•˜๊ณ  ๋ฒ—์–ด๋‚  ๋•Œ ๋ฐœ์ƒ
    • ํผ ์ œ์ถœ: ๋ชจ๋“  ํ•„๋“œ์— ๋Œ€ํ•œ ๊ฒ€์ฆ ์ˆ˜ํ–‰

  1. ๊ฒ€์ฆ ํŠธ๋ฆฌ๊ฑฐ ๋ณ€๊ฒฝ ๋ฐฉ๋ฒ•:

    • validate ํŒŒ์ผ์—์„œ configure ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ฒ€์ฆ ํŠธ๋ฆฌ๊ฑฐ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ.
    • ๋„ค ๊ฐ€์ง€ ์˜ต์…˜(validateOnBlur, validateOnChange, validateOnInput, validateOnModelUpdate) ์„ค์ •์œผ๋กœ ๊ธฐ๋ณธ ๋™์ž‘ ๋ณ€๊ฒฝ.
  2. ๊ฐ ์˜ต์…˜์˜ ์„ค๋ช…:

    • validateOnBlur: blur ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ ๊ฒ€์ฆ. ๊ธฐ๋ณธ๊ฐ’์€ true.
    • validateOnChange: change ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ ๊ฒ€์ฆ. ๊ธฐ๋ณธ๊ฐ’์€ true.
    • validateOnInput: ์ž…๋ ฅ ๋ณ€๊ฒฝ ์‹œ ์ฆ‰์‹œ ๊ฒ€์ฆ. ๊ฐ€์žฅ ๊ณต๊ฒฉ์ ์ธ ๊ฒ€์ฆ ๋ฐฉ์‹. ๊ธฐ๋ณธ๊ฐ’์€ false.
    • validateOnModelUpdate: v-model์„ ํ†ตํ•œ ๋‚ด๋ถ€ ๊ฐ’ ๋ณ€๊ฒฝ ์‹œ ๊ฒ€์ฆ. ๊ธฐ๋ณธ๊ฐ’์€ true.
  3. ๊ธฐ๋ณธ ๋™์ž‘์˜ ์ ์šฉ:

    • ํ˜„์žฌ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ๊ธฐ๋ณธ๊ฐ’์ด ์ ํ•ฉํ•˜์—ฌ ๊ธฐ๋ณธ ๋™์ž‘ ์‚ฌ์šฉ.
    • ํ•„์š”์— ๋”ฐ๋ผ ๊ฒ€์ฆ ํŠธ๋ฆฌ๊ฑฐ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Œ.
  4. ๋‹ค์Œ ๊ฐ•์˜ ๊ณ„ํš:

    • ๋‹ค์Œ ๊ฐ•์˜์—์„œ ๊ฒ€์ฆ์— ๋Œ€ํ•œ ์ž‘์—… ๊ณ„์† ์ง„ํ–‰ ์˜ˆ์ •.
src/includes/validation.ts
import {
  Form as VeeForm,
  Field as VeeField,
  defineRule,
  ErrorMessage,
  configure
} from 'vee-validate'
import {
  required,
  min,
  max,
  alpha_spaces as alphaSpaces,
  email,
  min_value as minVal,
  max_value as maxVal,
  confirmed,
  not_one_of as excluded
} from '@vee-validate/rules'
import type { App } from 'vue'

export default {
  // install(app: App, options?: { [key: string]: any }) {}
  install(app: App) {
    app.component('VeeForm', VeeForm)
    app.component('VeeField', VeeField)
    app.component('ErrorMessage', ErrorMessage)

    defineRule('required', required)
    defineRule('tos', required)
    defineRule('min', min)
    defineRule('max', max)
    defineRule('alpha_spaces', alphaSpaces)
    defineRule('email', email)
    defineRule('min_value', minVal)
    defineRule('max_value', maxVal)
    defineRule('passwords_mismatch', confirmed)
    defineRule('excluded', excluded)
    defineRule('country_excluded', excluded)

    configure({
      generateMessage(ctx) {
        const messages: { [key: string]: string } = {
          required: `The field ${ctx.field} is required.`,
          min: `The field ${ctx.field} is too short.`,
          max: `The field ${ctx.field} is too long.`,
          alpha_spaces: `The field ${ctx.field} may only contain alphabetical characters and space.`,
          email: `The field ${ctx.field} must be a valid email.`,
          min_value: `The field ${ctx.field} is too low.`,
          max_value: `The field ${ctx.field} is too high.`,
          excluded: `You are not allowed to use this value for the field ${ctx.field}`,
          country_excluded: `Due to restrictions, we do not accept users from this location.`,
          passwords_mismatch: `The passwords don't match.`,
          tos: `You must accept the Terms of Services.`
        }

        let message = ''
        const name = ctx.rule?.name

        if (typeof name === 'string') {
          const messagesName = messages[name]
          message = messagesName ? messagesName : `The field ${ctx.field} is invalid`
        }

        return message
      },
      validateOnBlur: true, // blur ์ด๋ฒคํŠธ์‹œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ // ๊ธฐ๋ณธ๊ฐ’ true
      validateOnChange: true, // change ์ด๋ฒคํŠธ์‹œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ // ๊ธฐ๋ณธ๊ฐ’ true
      validateOnInput: false, // input ์ด๋ฒคํŠธ์‹œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ // ๊ธฐ๋ณธ๊ฐ’ false
      validateOnModelUpdate: true // v-model ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ // ๊ธฐ๋ณธ๊ฐ’ true
    })
  }
}

































































ย 
ย 
ย 
ย