In Formik, I need validation errors to be displayed in both cases:
- On Change/Blur, individually as fields get touched on the form -- not all at once, during the filling-out process
- On clicking Submit at any time: here, all errors should be shown at once.
(1) is working but (2) is not. When I come to the form and just click the Submit button, nothing happens and the errors aren't displayed. The errors only get displayed when I touch the controls.
I think the problem is that to satisfy (1), my controls have
isInvalid={touched.startDate && errors.startDate}
But when I click Submit immediately, the control hasn't been touched yet, so this isInvalid
condition fails. But I can't remove the touched.startDate
part because otherwise, all invalid controls always start showing when filling out the form -- even the ones I haven't been touched. I need to keep my touched
requirement when filling out, but also show all errors on Submit. The Submit is the one case where all errors have to be shown at once. Is it possible to pass some submitClicked
variable somewhere to achieve this?
<Formik enableReinitialize
validationSchema={schema}
onSubmit={ (values, { validate }) => {
alert(JSON.stringify(values, null, 2));
}}
initialValues={{}}
>
{({
handleSubmit,
handleChange,
handleBlur,
values,
touched,
isValid,
errors,
}) => (
<Form onSubmit={handleSubmit}>
...
{/* EXAMPLE */}
<Form.Control type="date"
name="expirationDate"
value={values.expirationDate}
onChange={handleChange}
onBlur={handleBlur}
isInvalid={touched.expirationDate && errors.expirationDate}
</Form.Control>
</Form>
...
// Yup Schema used for form validation
const schema = yup.object().shape({
expirationDate: yup.date().required('Expiration Date is required'),
frequencyDays: yup.number().required('Frequency is required'),
interval: yup.string().required('Frequency interval is required'),
...