-
Hi, I try to implement dynamic radio group with validation. I would like to show error below the last input in component. I use <RadioGroup :values="[1,2,3]" name="example">
<RadioInput/>
<RadioGroup> // RadioGroup.vue
<template>
<div>
<!-- stuff -->
<slot v-for="..."/>
<span v-if="error">{{error}}</span>
</div>
</template>
<script setup>
// defineProps
const fieldError = useFieldError(name);
const isTouched = useIsFieldTouched(name);
const error = computed(() => isTouched.value ? fieldError.value : null);
// provide name & values to child
</script> Have you any ideas how to implement this correctly? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I can't use |
Beta Was this translation helpful? Give feedback.
-
I think you can reverse the relationship between I have something working along those lines, maybe it will work for you in that case. // RadioGroup setup
import { useField } from 'vee-validate';
// No need to provide `type=radio` since this is not an individual radio item
// now radio group have access to errors and can display them.
const field = useField('name', null)
// RadioInput Setup
import { inject, computed } from 'vue';\
// This is not documented yet
import { FieldContextKey } from 'vee-validate';
// if the field is null then no group was created
const field = inject(FieldContextKey, null);
// if the current item is checked
const isChecked = computed(() => field?.value.value === props.value);
// whenever the radio is clicked
field.handleChange(val); |
Beta Was this translation helpful? Give feedback.
I think you can reverse the relationship between
RadioInput
andRadioGroup
. SinceRadioInput
cannot be used without a correspondingRadioGroup
thenuseField
should be called inRadioGroup
. TheRadioInput
components would only just change their group parent value using either custom injections, actually vee-validate gives you a context key you can use to do that.I have something working along those lines, maybe it will work for you in that case.