|
| 1 | +--- |
| 2 | +outline: [2,3] |
| 3 | +--- |
| 4 | +# useValidation <Badge type="tip" text=">=2.7.0"/> |
| 5 | +> Used to validate a field against validators defined in a field's schema |
| 6 | +
|
| 7 | +## Usage |
| 8 | +::: code-group |
| 9 | +```Vue [Vue] |
| 10 | +<script setup> |
| 11 | +import { toRefs } from 'vue' |
| 12 | +import { |
| 13 | + useValidation, |
| 14 | + useFieldProps, |
| 15 | + useFormModel, |
| 16 | + useFieldEmits, |
| 17 | + useFieldAttributes |
| 18 | +} from '@kevinkosterr/vue3-form-generator' |
| 19 | +
|
| 20 | +const props = defineProps(useFieldProps()) |
| 21 | +const emits = defineEmits(useFieldEmits()) |
| 22 | +
|
| 23 | +const { field, model } = toRefs(props) |
| 24 | +const { isRequired, isDisabled, isReadonly } = useFieldAttributes(model.value, field.value) |
| 25 | +const { currentModelValue } = useFormModel(model.value, field.value) |
| 26 | + |
| 27 | +const { validate, errors, onChanged, onBlur } = useValidation( |
| 28 | + model.value, |
| 29 | + field.value, |
| 30 | + currentModelValue, |
| 31 | + props.formOptions, |
| 32 | + emits, |
| 33 | + isDisabled.value, |
| 34 | + isRequired.value, |
| 35 | + isReadonly.value |
| 36 | +) |
| 37 | +</script> |
| 38 | +``` |
| 39 | +```Vue [Vue TS] |
| 40 | +<script setup lang="ts"> |
| 41 | +import { toRefs } from 'vue' |
| 42 | +import { |
| 43 | + type FieldEmits, |
| 44 | + type FieldProps, |
| 45 | + type FieldBase, |
| 46 | + type FieldPropRefs, |
| 47 | + useValidation, |
| 48 | + useFieldAttributes, |
| 49 | + useFormModel |
| 50 | +} from '@kevinkosterr/vue3-form-generator' |
| 51 | +
|
| 52 | +type CustomField = FieldBase & { |
| 53 | + customAttr: string |
| 54 | +} |
| 55 | +
|
| 56 | +const props = defineProps<FieldProps<CustomField>>() |
| 57 | +const emits = defineEmits<FieldEmits>() |
| 58 | + |
| 59 | +const { field, model }: FieldPropRefs<CustomField> = toRefs(props) |
| 60 | +const { isRequired, isDisabled, isReadonly } = useFieldAttributes(model.value, field.value) |
| 61 | +const { currentModelValue } = useFormModel(model.value, field.value) |
| 62 | +
|
| 63 | +const { validate, errors, onChanged, onBlur } = useValidation( |
| 64 | + model.value, |
| 65 | + field.value, |
| 66 | + currentModelValue, |
| 67 | + props.formOptions, |
| 68 | + emits, |
| 69 | + isDisabled.value, |
| 70 | + isRequired.value, |
| 71 | + isReadonly.value |
| 72 | +) |
| 73 | +</script> |
| 74 | +``` |
| 75 | +::: |
| 76 | + |
| 77 | +## Arguments |
| 78 | + |
| 79 | +### `model` <Badge type="info" text="Object"/> |
| 80 | +Model object, as returned by the props |
| 81 | + |
| 82 | +### `field` <Badge type="info" text="Object"/> |
| 83 | +Field schema object, as returned by the props |
| 84 | + |
| 85 | +### `currentModelValue` <Badge type="info" text="Ref<any>"/> |
| 86 | +`Ref` of the current value from the field. Returned by [`useFormModel`](/guide/composables/useFormModel). |
| 87 | + |
| 88 | +### `formOptions` <Badge type="info" text="Object"/> |
| 89 | +Form options object, as returned by the props. |
| 90 | + |
| 91 | +### `emits` <Badge type="info" text="EmitFn<FieldEmits>"/> |
| 92 | +Emit function as returned by `defineEmits()` |
| 93 | + |
| 94 | +### `isDisabled` <Badge type="info" text="Boolean"/> |
| 95 | +Whether the field is disabled, can be obtained from [`useFieldAttributes()`](/guide/composables/useFieldAttributes) |
| 96 | + |
| 97 | +### `isRequired` <Badge type="info" text="Boolean"/> |
| 98 | +Whether the field is required, can be obtained from [`useFieldAttributes()`](/guide/composables/useFieldAttributes) |
| 99 | + |
| 100 | +### `isReadonly` <Badge type="info" text="Boolean"/> |
| 101 | +Whether the field is readonly, can be obtained from [`useFieldAttributes()`](/guide/composables/useFieldAttributes) |
| 102 | + |
| 103 | +## Returns |
| 104 | + |
| 105 | +### `errors` <Badge type="info" text="string[]"/> |
| 106 | +An array of errors for the current field. Will be auto-updated on every validation cycle. Must be cleared |
| 107 | +manually when the value of a field has changed. |
| 108 | + |
| 109 | +### `validate` <Badge type="info" text="Function"/> |
| 110 | +A validation function, meant to be called when a validation has to take place. Used when a field is always validated |
| 111 | +at the same moment and isn't affected by validation triggers, such as `'onChanged'` or `'onBlur'`. |
| 112 | + |
| 113 | +### `onChanged` <Badge type="info" text="Function"/> |
| 114 | +A wrapped validation function, only validates when `'onChanged'` validation is enabled. Should always be called if the value is changed, unless the field is unaffected by |
| 115 | +validation triggers. |
| 116 | + |
| 117 | +### `onBlur` <Badge type="info" text="Function"/> |
| 118 | +A wrapped validation function, only validates when `'onBlur'` validation is enabled (which is the default behavior). Should always be called if the value is changed, unless the field is unaffected by |
| 119 | +validation triggers. |
0 commit comments