This repository was archived by the owner on Jul 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 820
Improve onError validation #1730
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
5563142
Introduce reason-based validation
dmtrKovalenko 70b66d4
Remove dead validation code
dmtrKovalenko e9a0687
Implement new validation for TimePicker and DateTimePicker
dmtrKovalenko 4f2207f
Remove redux form docs example
dmtrKovalenko 868358a
Add Formik with validation schema example
dmtrKovalenko 679ea8d
More tests
dmtrKovalenko a501984
TimePicker validation tests
dmtrKovalenko a1a6b42
Move parsing min/max date up to the root component
dmtrKovalenko 5ca8ef2
Use touched state in the formik example
dmtrKovalenko 3af9888
Remove console.logs
dmtrKovalenko 71981e0
Merge conflicts
dmtrKovalenko cbe5fc4
Fix lint and build errors
dmtrKovalenko 3eddd8b
Remove visual regression flakiness with time validation
dmtrKovalenko f21609c
Remove emptyInputText
dmtrKovalenko 754a95b
Fix validation tests
dmtrKovalenko 58f12c7
Commit .size-snapshot
dmtrKovalenko e349496
Implement validation for DateRangePicker.tsx
dmtrKovalenko 5bce1e4
Add DateRange validation tests
dmtrKovalenko e4166a7
Fix linter
dmtrKovalenko 860d6f5
Fix broken design of date rangepicker input parsing
dmtrKovalenko c73d4e7
Merge conflicts
dmtrKovalenko 8dedc04
Remove <Code> from formik examples
dmtrKovalenko 66eb022
Update yarn.lock
dmtrKovalenko af381ef
Update docs/pages/guides/FormikOurValidation.example.tsx
dmtrKovalenko 1201eef
Update docs/pages/guides/FormikOurValidation.example.tsx
dmtrKovalenko 79cea16
Update docs/pages/guides/FormikOurValidation.example.tsx
dmtrKovalenko 806b63c
Update new forms example to be more consolidated with @materail-ui/core
dmtrKovalenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import React from 'react'; | ||
| import Grid from '@material-ui/core/Grid'; | ||
| import TextField from '@material-ui/core/TextField'; | ||
| import { Formik, Form, Field, FieldProps } from 'formik'; | ||
| import { format, isWeekend, isWednesday } from 'date-fns'; | ||
| import { DatePicker, DatePickerProps } from '@material-ui/pickers'; | ||
|
|
||
| interface DatePickerFieldProps extends FieldProps, DatePickerProps { | ||
| getShouldDisableDateError: (date: Date) => string; | ||
| } | ||
|
|
||
| function DatePickerField({ | ||
| form, | ||
| field: { value, name }, | ||
| maxDate = new Date('2100-01-01'), | ||
| minDate = new Date('1900-01-01'), | ||
| getShouldDisableDateError, | ||
| ...other | ||
| }: DatePickerFieldProps) { | ||
| const currentError = form.errors[name]; | ||
| const toShowError = Boolean(currentError && form.touched[name]); | ||
|
|
||
| return ( | ||
| <DatePicker | ||
| autoOk | ||
| clearable | ||
| minDate={minDate} | ||
| maxDate={maxDate} | ||
| value={value} | ||
| onError={(reason, value) => { | ||
| switch (reason) { | ||
| case 'invalidDate': | ||
| form.setFieldError(name, 'Invalid date format'); | ||
| break; | ||
|
|
||
| case 'disablePast': | ||
| form.setFieldError(name, 'Values in the past are not allowed'); | ||
| break; | ||
|
|
||
| case 'maxDate': | ||
| form.setFieldError(name, `Date should not be after ${format(maxDate, 'P')}`); | ||
| break; | ||
|
|
||
| case 'minDate': | ||
| form.setFieldError(name, `Date should not be after ${format(maxDate, 'P')}`); | ||
| break; | ||
|
|
||
| case 'shouldDisableDate': | ||
| // shouldDisableDate returned true, render custom message according to the `shouldDisableDate` logic | ||
| form.setFieldError(name, getShouldDisableDateError(value)); | ||
| break; | ||
|
|
||
| default: | ||
| form.setErrors({ | ||
| ...form.errors, | ||
| [name]: undefined, | ||
| }); | ||
| } | ||
| }} | ||
| // Make sure that your 3d param is set to `false` on order to not clear errors | ||
| onChange={date => form.setFieldValue(name, date, false)} | ||
| renderInput={props => ( | ||
| <TextField | ||
| {...props} | ||
| name={name} | ||
| error={toShowError} | ||
| helperText={toShowError ? currentError ?? props.helperText : undefined} | ||
| // Make sure that your 3d param is set to `false` on order to not clear errors | ||
| onBlur={() => form.setFieldTouched(name, true, false)} | ||
| /> | ||
| )} | ||
| {...other} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| function validateDatePickerValue(date: Date) { | ||
| if (isWeekend(date)) { | ||
| return 'Weekends are not allowed'; | ||
| } | ||
|
|
||
| if (isWednesday(date)) { | ||
| return 'Wednesdays are not allowed'; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| export default function FormikExample() { | ||
| return ( | ||
| <Formik onSubmit={console.log} initialValues={{ date: new Date() }}> | ||
| {({ values, errors }) => ( | ||
| <Form> | ||
| <Grid container> | ||
| <Grid item container justify="center" xs={12}> | ||
| <Field | ||
| name="date" | ||
| disablePast | ||
| component={DatePickerField} | ||
| shouldDisableDate={(date: Date) => validateDatePickerValue(date) !== null} | ||
| getShouldDisableDateError={validateDatePickerValue} | ||
| /> | ||
| </Grid> | ||
|
|
||
| <Grid item xs={12} sm={12} style={{ margin: '24px' }}> | ||
| <pre> | ||
| <code>{JSON.stringify({ errors, values }, null, 2)}</code> | ||
| </pre> | ||
| </Grid> | ||
| </Grid> | ||
| </Form> | ||
| )} | ||
| </Formik> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import React from 'react'; | ||
| import { date, object } from 'yup'; | ||
| import { Grid } from '@material-ui/core'; | ||
| import { TextField } from '@material-ui/core'; | ||
| import { Formik, Form, Field, FieldProps } from 'formik'; | ||
| import { DatePicker, BaseDatePickerProps } from '@material-ui/pickers'; | ||
|
|
||
| interface DatePickerFieldProps extends FieldProps, BaseDatePickerProps { | ||
| getShouldDisableDateError: (date: Date) => string; | ||
| } | ||
|
|
||
| function DatePickerField({ | ||
| field, | ||
| form, | ||
| maxDate = new Date('2100-01-01'), | ||
| minDate = new Date('1900-01-01'), | ||
| getShouldDisableDateError, | ||
| ...other | ||
| }: DatePickerFieldProps) { | ||
| const currentError = form.errors[field.name]; | ||
|
|
||
| return ( | ||
| <DatePicker | ||
| autoOk | ||
| clearable | ||
| minDate={minDate} | ||
| maxDate={maxDate} | ||
| value={field.value} | ||
| // Make sure that your 3d param is set to `true` in order to run validation | ||
| onChange={date => form.setFieldValue(field.name, date, true)} | ||
| renderInput={props => ( | ||
| <TextField | ||
| name={field.name} | ||
| {...props} | ||
| error={Boolean(currentError)} | ||
| helperText={currentError ?? props.helperText} | ||
| // Make sure that your 3d param is set to `true` in order to run validation | ||
| onBlur={() => form.setFieldTouched(name, true, true)} | ||
| /> | ||
| )} | ||
| {...other} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| const schema = object({ | ||
| date: date() | ||
| .required() | ||
| .min(new Date()) | ||
| .max(new Date('2100-10-10')), | ||
| }); | ||
|
|
||
| export default function FormikValidationSchemaExample() { | ||
| return ( | ||
| <Formik validationSchema={schema} onSubmit={console.log} initialValues={{ date: new Date() }}> | ||
| {({ values, errors }) => ( | ||
| <Form> | ||
| <Grid container> | ||
| <Grid item container justify="center" xs={12}> | ||
| <Field name="date" disablePast component={DatePickerField} /> | ||
| </Grid> | ||
|
|
||
| <Grid item xs={12} sm={12} style={{ margin: '24px' }}> | ||
| <pre> | ||
| <code>{JSON.stringify({ errors, values }, null, 2)}</code> | ||
| </pre> | ||
| </Grid> | ||
| </Grid> | ||
| </Form> | ||
| )} | ||
| </Formik> | ||
| ); | ||
| } |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.