I have searched around, not sure if this is possible. Essentially I am wanting to validate a formik form with YUP using a state hook that is not a form value.
validationSchema={Yup.object({
comments: Yup.string()
.when(approvalState, {
is: false,
then: Yup.string().required('Comments are required when denying an approval.'),
}),
})}
Here is the form field, it is essentially a text box:
Here is the submit button, which changes the state value to true:
<button className='buttonPrimary' type='submit' onClick={()=> setApprovalState(true)} disabled={formik.isSubmitting}>Approve</button>
Here is the other button which also submits, changes the state value to false:
<button className={`buttonSecondary ${styles.marginRight}`} type='submit' onClick={()=> setApprovalState(false)} disabled={formik.isSubmitting}>Deny</button>
The state value updates fine, any way to conditionally validate the textbox as required based on a piece of state?