Created
January 20, 2024 01:44
-
-
Save lmiller1990/cfb703d79589ebeda3802a74b071c078 to your computer and use it in GitHub Desktop.
Vaildation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<input v-model="formData.name" /> | |
<p v-if="!formStatus.name.valid">{{ formStatus.name.message }}</p> | |
</template> | |
<script setup lang="ts'> | |
const formData = reactive({ | |
name | |
}) | |
/** Assume all valid by default */ | |
const formStatus = ref<{ name: { valid: boolean, message?: string }>({ | |
name: { value: true } | |
}) | |
watch(formData, async (newData) => { | |
const serverValidated = await fetch("/validate, { method: "POST", formData: newData }) | |
formStatus.value = serverValidated /* could be { name: { valid: false, message: 'Too short' } } */ | |
}) | |
const formState = computed(() => { | |
return { | |
name: { | |
value: formData.name | |
} | |
} | |
}) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment