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
<Field
type="checkbox"
class="form-check-input form-check-sm "
:name="`sku[${index}].cross_dock_check`"
:id="`sku[${index}].cross_dock_check`"
/>
<Field :name="`sku[${index}].pickup_address`" v-slot="{ field }" :value="store.pickup_address_id">
<Multiselect v-model="field.value"
:searchable="true"
v-bind="field"
:options="add_sku_data.get_pickup_addresses"
placeholder="Address"
></Multiselect>
</Field>
sku: Yup.array().of(Yup.object().shape({pickup_address: Yup.number().required().typeError("Address is required")}))
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.
https://codesandbox.io/s/6i6vv?file=/src/index.js:1260-1264
Regards,
Lauris Stepanovs,
Keenthemes Support Team