ããã«ã¡ã¯ãContractSã§ããã³ãã¨ã³ãã¨ã³ã¸ãã¢ããã¦ããååã§ãã
å¼ç¤¾ã§ã¯ãUIã®ããªãã¼ã·ã§ã³ã¨ãã¦VeeValidate v4
ãæ¡ç¨ãã¦ãã¾ãã
ãã®è¨äºã§ã¯ãè¤æ°ã®ã¤ã³ãããã1ã¤ã®ããªãã¼ã·ã§ã³ãã£ã¼ã«ãã¨ãã¦æ±ãæ¹æ³ã«ã¤ãã¦è§£æ±ºçãæ示ãããã¨æãã¾ãã
ç®æ¬¡
åæç¥è
ä»åã®è¨äºã®åæç¥èã§ããVeeValidate v4ã«ã¤ãã¦ãåç¥ã®æ¹ã¯ã¹ã«ã¼ãã¦ãã ããã
VeeValidateã¨ã¯ï¼
Vue.jsã«ããããã©ã¼ã ããªãã¼ã·ã§ã³ãç°¡ç´ åãã人æ°ã®ã©ã¤ãã©ãªã§ãã
æ¥æ¬èªããã¥ã¡ã³ããå
å®ãã¦ããã大å¤æ±ãããããã®ã«ãªã£ã¦ãã¾ãã
vee-validate.logaretm.com
VeeValidate v4 ã® Compositioné¢æ°
1. useField
const { value, errorMessage } = useField(() => 'email', yup.string().email().required(), )
useField
ã¯ãã³ã³ãã¼ãã³ãããã£ã¼ã«ãã¨ãã¦æ±ããã¨ãã§ããé¢æ°ã§ãã
v-model
ã«bindãã¦ä½¿ãå®éã®å
¥åå¤value
ããããªãã¼ã·ã§ã³ã«ã¼ã«ã«ããããªãå ´åã«çæãããerrorMeesage
ãªã©ãæãã¦ãã¾ãã
vee-validate.logaretm.com
2. useForm
const { handleSubmit } = useForm()
useForm
ã¯ãè¨è¿°ããã³ã³ãã¼ãã³ãããã©ã¼ã ã¨ãã¦æ±ããã¨ãã§ããé¢æ°ã§ãã
åè¿°ããuseField
ãæããã³ã³ãã¼ãã³ããåã³ã³ãã¼ãã³ã以ä¸ã«æã¤å ´åã«ç¨ãããã¨ã§ããã®å¤ãåå¾ãã¦éä¿¡ã§ããhandleSubmit
ãªã©ãæãã¦ãã¾ãã
vee-validate.logaretm.com
ãªãå¿ è¦ãªã®ãï¼
å¼ç¤¾ã®ãããã¯ãã§ã¯ãå¥ç´æ¸ã管çããã«å½ãã£ã¦å¥ç´é
ç®
ã¨å¼ã°ããé¢é£æ
å ±ããåå¥ç´æ¸ãæãã¦ãã¾ãã
ããããã¦ã¼ã¶ã®æ¹ã¸å
¥åãã¦ããã ãå ´åã«å¿
é é
ç®ã¨ããããã¿ã¼ã³ãåå¨ãã¾ãã
ãã®ã¨ãã以ä¸ã®ãããªå¥ã ã®ãã£ã¼ã«ãããã¨ã©ã¼ã¡ãã»ã¼ã¸ã1ç®æã«éç´ãã¦ããªãã¼ã·ã§ã³ãããããã¿ã¼ã³ãåºã¦ãã¾ããã
ãã®ãã£ã¼ã«ãã¯ãå¥ç´æ¸ã«å¯¾ããèªåæ´æ°æ©è½ãæå¹ã«ããã¨ããå¥ç´æ¸ã®å¥ç´æéçµäºå¾ã«æ°ããå¼ã延ã°ãå¥ç´æéã®é·ããå
¥åãããã£ã¼ã«ãã§ãã
å·¦å´ã¯å¤ï¼åè§æ°åï¼ã®ã¤ã³ããããå³å´ã¯åä½ï¼æ¥ãã¶æãå¹´ããé¸æï¼ã®ãã«ãã¦ã³ã¨ãªã£ã¦ãããï¼ã¤ã§ï¼ã¤ã§ãã
ãããVeeValidateã®ä»æ§ä¸ããã®ãããªãã¿ã¼ã³ã§ã¯å·¦å³ããããã®ãã£ã¼ã«ãããåã ã®ããªãã¼ã·ã§ã³ãæã¤å¥ã®ãã£ã¼ã«ãå¤ã¨ãã¦æ±ããã¦ãã¾ããããä¸ã®ç»åã®ããã«ã¨ã©ã¼ã¡ãã»ã¼ã¸ã®è¡¨ç¤ºç®æãéç´ã§ãã¾ããã
ãªãã¨ã解決ããæ段ãèããçµæããã®è¨äºã«ãªãã¾ãã
TL;DR
- ã¨ã©ã¼ã¡ãã»ã¼ã¸ãèªèº«ã®ç®¡ç対象ã¨ãã¦å ããé¢æ°ã
provide
ããã©ããã¼ã³ã³ãã¼ãã³ããä½æ useField
ãæã¤ã³ã³ãã¼ãã³ããã1. ã®ã³ã³ãã¼ãã³ãããprovide
ãããé¢æ°ãinject
ãã¦ã¨ã©ã¼ã¡ãã»ã¼ã¸ã管ç対象ã«å ãã
以ä¸ã®æµãã§å®ç¾ãã¾ãã
ãã£ã¦ã¿ã
åè
VeeValidate v4éçºè ãAbdelrahman Awadããã®CodeSandboxã«ãã£ã以ä¸ã®å 容ãåèã«ãã¾ããã
å®è£ ä¾
1. ã©ããã¼ã³ã³ãã¼ãã³ãã®ä½æ
ã¾ã以ä¸ã®ããã«ãåçã«useFieldã®å¤ãè¤æ°ç®¡çããããã®é¢æ°ãæä¾ããã©ããã¼ã³ã³ãã¼ãã³ããå®è£ ãã¾ãã
script
// Types interface Injection { errorMessage: Ref<string> // ä»ã«ç®¡çããã対象ãããã°è¿½å ãã } export type ProvideAddField = (injection: Injection) => void // Variables const errorMessages = ref<Ref<string>[]>([]) // Computed const allErrorMessages = computed<string[]>(() => (errorMessages.value.map(it => it.value).filter(it => !!it))) /** * ã¨ã©ã¼ã¡ãã»ã¼ã¸ãç»é²ããé¢æ°ãprovideãã */ onBeforeMount(() => { provide(ProvideKey.INCLUDE_FIELD_ERROR_MESSAGE, (injection: Injection) => { errorMessages.value.push(injection.errorMessage) }, ) })
onBeforeMount
ã®ã©ã¤ããµã¤ã¯ã«ã§ããã®ã³ã³ãã¼ãã³ãã¸ã¨ã©ã¼ã¡ãã»ã¼ã¸ãç»é²ããé¢æ°ãprovide
ãã¦ãããããã«ãã£ã¦è¿½å ãããã¨ã©ã¼ã¡ãã»ã¼ã¸ç¾¤ã®errorMessages
ãæã£ã¦ããã¾ãã
ã¾ããå®éã«ã¨ã©ã¼ãçºçãã¦ãããã®ã ãã«çµãè¾¼ãã allErrorMessages
ãåããã¦æã£ã¦ããã¾ãã
template
<template> <div> <slot /> <!-- è¤æ°ã®ã¡ãã»ã¼ã¸ã表示ãããå ´åã¯v-forããã°è¯ã --> <AtomsErrorMessage v-show="allErrorMessages.length !== 0" class="mt-1" :message="allErrorMessages[0]" /> </div> </template>
template
é¨ã«ã¯ãprovide
ããé¢æ°ã«ãã追å ãããã¨ã©ã¼ã¡ãã»ã¼ã¸ã®è¡¨ç¤ºç®æã¨ãå½è©²ã³ã³ãã¼ãã³ãã¸ã¨ã©ã¼ã¡ãã»ã¼ã¸ã追å ããããã£ã¼ã«ãã³ã³ãã¼ãã³ããå
¥ãè¾¼ãããã®slot
ãè¨è¿°ãã¾ãã
2. ãã£ã¼ã«ãã³ã³ãã¼ãã³ãããé¢æ°ãinject
script
const { value, errorMessage } = useField(() => 'email', yup.string().email().required(), ) // (ä¸ç¥) /** * ã¨ã©ã¼ã¡ãã»ã¼ã¸ããã©ããã¼ã³ã³ãã¼ãã³ãã®ç®¡ç対象ã¨ãã¦è¿½å ãã¾ã â» */ const includeField = (errorMessage: Ref<string>) => { const injection: ProvideAddField | undefined = inject(ProvideKey.INCLUDE_FIELD_ERROR_MESSAGE) if (injection) injection({ errorMessage }) } onMounted(() => { includeField('email', errorMessage) })
ãã£ã¼ã«ãã³ã³ãã¼ãã³ãã¸ãonBeforeMounted
ã§provide
ããã¦ããé¢æ°ãã次ã®onMounted
ã©ã¤ããµã¤ã¯ã«ã§å¼åºãå¦çã追å ãã¦ããã¾ãã
â» ããã§ã¯ä¾¿å®ä¸ç´ ã§å®ç¾©ãã¦ãã¾ãããcomposablesã«ãã¦ããã®ãè¯ãã§ã
3. å®éã«ä½¿ã£ã¦ã¿ã
ã©ããã¼ã³ã³ãã¼ãã³ãã®slotã¸ãå®éã«ããªãã¼ã·ã§ã³ããããã£ã¼ã«ãã³ã³ãã¼ãã³ããå ¥ããã¿ã¾ãã
ããã«ãããã¨ã©ã¼ãçºçãããå ´åã«1ç®æã¸éç´ãã¦è¡¨ç¤ºã§ããããã«ãªãã¾ãã
ã¾ã¨ã
å®è£
èªä½ã«æéã¯ãããã¾ãããä¸åº¦ä»çµã¿åãã¦ãã¾ãã°æ±ç¨çã«å©ç¨ã§ããããå©ä¾¿æ§ã¯é«ãã¨æãã¾ãã
ããå°ãç°¡ç´ ã«å®ç¾ã§ããæ¹æ³ãããã¾ãããããã²ãææãã ããã
ä»åã®è¨äºããå°ãã§ãåããããªèª²é¡ãæ±ãã¦ããæ¹ã®å©ãã«ãªãã°å¹¸ãã§ãã
æå¾ã«
ãèªã¿ããã ããããã¨ããããã¾ããã
ContractSã§ã¯ä¸ç·ã«ãããã¯ããé²åããã¦ããã¨ã³ã¸ãã¢ãåéä¸ã§ãã