I have additional fields that only gets shown if a checkbox is selected (Boolean value is true). These additional fields also need validation but using the .when() method doesn't seem to react to the checkbox being true as a condition. The validation part of my code looks like:
const schema = useMemo(
() =>
yup
.object()
.shape({
type: yup
.string()
.label(translate('Forms.EmailForm.type.label'))
.required(),
person: yup
.object()
.shape({
firstName: yup
.string()
.required()
.trim()
.min(1)
.max(255)
.label(
translate(
'Forms.EmailForm.firstName.label',
),
),
hasAddress: yup.boolean(),
address: yup.boolean().when('hasAddress', {
is: true,
then: yup
.object()
.shape({
firstLine: yup
.string()
.required()
.max(255)
.label(
translate(
'Forms.EmailForm.address.firstLine.label',
),
),
secondLine: yup
.string()
.max(255)
.label(
translate(
'Forms.EmailForm.address.secondLine.label',
),
),
}),
}),
}),
}),
[],
);
const form = useForm<EmailForm>({
resolver: yupResolver(schema),
defaultValues: {
type: 'PERSON',
},
});