Skip to content

useFieldValidate 2.0.0+

Used to validate a field against validators defined in a fields schema

Usage

vue
<script setup>
import { toRefs } from 'vue'
import { useFieldValidate, useFieldProps } from '@kevinkosterr/vue3-form-generator'

const props = defineProps(useFieldProps())

const { field, model } = toRefs(props)
  
const { validate } = useFieldValidate(model.value, field.value)
</script>

Arguments

model Object

Model object, as returned by the props

field Object

Field schema object, as returned by the props

Returns

validate Function

Validates the field, existing fields typically use this in an onBlur()

  • Arguments:
    • currentModelValue: any - the current value of the field.
  • Returns: Promise<string[]> - an array of error messages, or empty array if no errors have been found.
javascript
validate(currentModelValue.value).then((validationErrors) => {
  //... 
})

errors string[]

An array of errors that have been found when validating the current value against all validators

WARNING

If you want your component to work properly with validation, you'll have to expose this value.