105. Validation Triggers
npm create vue@latest
๊ฒ์ฆ ํธ๋ฆฌ๊ฑฐ์ ๋ํ ์ค๋ช
validate
๋ ๋ค์ํ ์ด๋ฒคํธ์์ ์ ๋ ฅ์ ๊ฒ์ฆํ๋๋ก ์ค์ ํ ์ ์์- ๊ธฐ๋ณธ์ ์ผ๋ก ๋ค ๊ฐ์ง ๊ฒฝ์ฐ์ ๊ฒ์ฆ์ด ์ํ๋จ:
change
์ด๋ฒคํธJavaScript
๋ฅผ ํตํ ๋ด๋ถ ๊ฐ ๋ณ๊ฒฝblur
์ด๋ฒคํธ- ํผ ์ ์ถ ์
๊ธฐ๋ณธ ๊ฒ์ฆ ๋์
change
์ด๋ฒคํธ: ์์ ๊ฐ์ ๋ณ๊ฒฝ์ ๋ฐ์JavaScript
๋ฅผ ํตํ ๊ฐ ๋ณ๊ฒฝ:v-model
์ง์๋ฌธ์ ์ด์ฉํ ๊ฐ ๋ณ๊ฒฝ ์ ๊ฒ์ฆ ๋ฐ์blur
์ด๋ฒคํธ: ์ฌ์ฉ์๊ฐ ์ ๋ ฅ ํ๋๋ฅผ ํฐ์นํ๊ณ ๋ฒ์ด๋ ๋ ๋ฐ์- ํผ ์ ์ถ: ๋ชจ๋ ํ๋์ ๋ํ ๊ฒ์ฆ ์ํ
๊ฒ์ฆ ํธ๋ฆฌ๊ฑฐ ๋ณ๊ฒฝ ๋ฐฉ๋ฒ:
- validate ํ์ผ์์ configure ํจ์๋ฅผ ํตํด ๊ฒ์ฆ ํธ๋ฆฌ๊ฑฐ ๋ณ๊ฒฝ ๊ฐ๋ฅ.
- ๋ค ๊ฐ์ง ์ต์ (validateOnBlur, validateOnChange, validateOnInput, validateOnModelUpdate) ์ค์ ์ผ๋ก ๊ธฐ๋ณธ ๋์ ๋ณ๊ฒฝ.
๊ฐ ์ต์ ์ ์ค๋ช :
- validateOnBlur: blur ์ด๋ฒคํธ ๋ฐ์ ์ ๊ฒ์ฆ. ๊ธฐ๋ณธ๊ฐ์ true.
- validateOnChange: change ์ด๋ฒคํธ ๋ฐ์ ์ ๊ฒ์ฆ. ๊ธฐ๋ณธ๊ฐ์ true.
- validateOnInput: ์ ๋ ฅ ๋ณ๊ฒฝ ์ ์ฆ์ ๊ฒ์ฆ. ๊ฐ์ฅ ๊ณต๊ฒฉ์ ์ธ ๊ฒ์ฆ ๋ฐฉ์. ๊ธฐ๋ณธ๊ฐ์ false.
- validateOnModelUpdate: v-model์ ํตํ ๋ด๋ถ ๊ฐ ๋ณ๊ฒฝ ์ ๊ฒ์ฆ. ๊ธฐ๋ณธ๊ฐ์ true.
๊ธฐ๋ณธ ๋์์ ์ ์ฉ:
- ํ์ฌ ํ๋ก์ ํธ์์๋ ๊ธฐ๋ณธ๊ฐ์ด ์ ํฉํ์ฌ ๊ธฐ๋ณธ ๋์ ์ฌ์ฉ.
- ํ์์ ๋ฐ๋ผ ๊ฒ์ฆ ํธ๋ฆฌ๊ฑฐ๋ฅผ ๋ณ๊ฒฝํ ์ ์์.
๋ค์ ๊ฐ์ ๊ณํ:
- ๋ค์ ๊ฐ์์์ ๊ฒ์ฆ์ ๋ํ ์์ ๊ณ์ ์งํ ์์ .
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
})
}
}
ย
ย
ย
ย