Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let formik do the default validateOnChange #651

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions src/components/form/LocationForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,12 @@ export const LocationForm = ({ editingId, initialValues }) => {
<StyledForm>
<Formik
validate={validateLocation}
validateOnChange={false}
initialValues={mergedInitialValues}
validateOnMount
onSubmit={isLoggedIn ? handleSubmit : onPresubmit}
>
{(formikProps) => {
const { isSubmitting, isValid, dirty } = formikProps
const isUploadingPhotos = formikProps.values.review.photos.some(
(p) => p.isUploading,
)
const formDirty = dirty || positionDirty

return (
Expand All @@ -254,9 +250,7 @@ export const LocationForm = ({ editingId, initialValues }) => {
Cancel
</Button>
<Button
disabled={
isSubmitting || !isValid || !formDirty || isUploadingPhotos
}
disabled={isSubmitting || !isValid || !formDirty}
type="submit"
>
{isSubmitting ? 'Submitting' : t('glossary.submit')}
Expand Down
8 changes: 1 addition & 7 deletions src/components/form/ReviewForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,26 +192,20 @@ export const ReviewForm = ({
<StyledForm>
<Formik
validate={({ review }) => validateReview(review)}
validateOnChange={false}
initialValues={initialValues}
validateOnMount
onSubmit={isLoggedIn ? handleSubmit : onPresubmit}
>
{(formikProps) => {
const { isSubmitting, isValid, dirty } = formikProps
const isUploadingPhotos = formikProps.values.review.photos.some(
(p) => p.isUploading,
)

return (
<Form>
<ReviewStep standalone hasHeading={editingId == null} />
<ProgressButtons>
<div style={{ textAlign: editingId ? 'center' : 'left' }}>
<Button
disabled={
isSubmitting || !isValid || !dirty || isUploadingPhotos
}
disabled={isSubmitting || !isValid || !dirty}
type="submit"
>
{isSubmitting ? 'Submitting' : t('glossary.submit')}
Expand Down
9 changes: 2 additions & 7 deletions src/utils/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,11 @@ export const reviewToForm = ({
const isEveryPhotoUploaded = (photos) =>
photos.every((photo) => !photo.isUploading)

const validateLocationStep = ({ types }) => {
export const validateLocation = ({ review, ...location }) => {
const errors = {}
if (types.length === 0) {
if (location.types.length === 0) {
errors.types = true
}
return errors
}

export const validateLocation = ({ review, ...location }) => {
const errors = validateLocationStep(location)

if (!isEmptyReview(review)) {
Object.assign(errors, validateReview(review))
Expand Down
Loading