I have dynamic fields what i want to is to validate one dynamic field on thee basis of other dynamic field with yup
this is dynamic field one
this is my second dynamic field
what i want to do is if the checkbox[0] is checked then the pickup_address[0] checkbox[1] is checked then the pickup_address[1] is required and so on
the validation for all address is working
sku: Yup.array().of(Yup.object().shape({pickup_address: Yup.number().required().typeError('Address is required')}))
so how should i be able to add condition , that address is required only if the checkbox is checked and please do tell how to handle v-model for dynamic fields
Hi Muhammad,
You can achieve this using the when function in your Yup schema.
const schema = Yup.object().shape({
checkboxName: Yup.boolean(),
address: Yup.string().when("checkboxName", {
is: true,
then: Yup.string().required("Address is required."),
}),
}); Hi Lauris,
thanks for the reply , the above mentioned solution is working when i am working with singer fields, like for only one checkbox and only one address,
however it is not working for dynamically added fields, as i have mentioned earlier in my post that
address : Yup.array().of(Yup.object().shape({pickup_address: Yup.number().required().typeError("Address is required")})) Hi Muhammad,
I don't know all your requirements, but it sounds like in this case you will need to generate your schema dynamically as well.
Here is another example of how you can generate dynamic yup schema.
Regards,
Lauris Stepanovs,
Keenthemes Support Team