Skip to content

Commit

Permalink
✨ [open-formulieren/open-forms#4699] Trigger AddressNL validation aft…
Browse files Browse the repository at this point in the history
…er dirty

To trigger the validation once the addressNL component has changed, we have to monitor formik changes. Using the `dirty` property from the `useFormikContext` we can simplify this monitoring. Unfortunately we can only set/alter the validation on the top level. With this we have three options:

- We can add `onBlur` properties to all addressNL fields. This means that we can handle formValidation after blur, and only when `dirty === true`. This would add a lot of custom properties and logic, for a small 'problem'. This would also move us futher from a "the component functions on its own" way of working.
- We can add `onBlur` eventListeners via `useEffect`. This would give us the same functionality as the previous option, without all the custom properties. It would also make the addressNL component even more complex and introduce unexpected behavior.
- Finally we can change the validation rule based on the `dirty` property. For this to work, we would have to pass the `dirty` prop to the parent component, to then change the validation rule. It is a strange way to work, but would mean the least amount of custom behavior and keep the validation process understandable.

For simplicity sake, I've chosen the last option. To use the `dirty` prop from the formik context to change the addressNL component validation
  • Loading branch information
robinmolen committed Jan 30, 2025
1 parent 74147f9 commit b16552c
Showing 1 changed file with 36 additions and 17 deletions.
53 changes: 36 additions & 17 deletions src/formio/components/AddressNL.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
*/
import {Formik, useFormikContext} from 'formik';
import debounce from 'lodash/debounce';
import {useContext, useEffect} from 'react';
import {useContext, useEffect, useState} from 'react';
import {createRoot} from 'react-dom/client';
import {Formio} from 'react-formio';
import {FormattedMessage, IntlProvider, defineMessages, useIntl} from 'react-intl';
import {z} from 'zod';
import {toFormikValidationSchema} from 'zod-formik-adapter';
import {toFormikValidate} from 'zod-formik-adapter';

import {ConfigContext} from 'Context';
import {get} from 'api';
Expand Down Expand Up @@ -299,6 +299,7 @@ const addressNLSchema = (required, intl, {postcode = {}, city = {}}) => {

const AddressNLForm = ({initialValues, required, deriveAddress, layout, setFormioValues}) => {
const intl = useIntl();
const [dirty, setDirty] = useState(false);

Check warning on line 302 in src/formio/components/AddressNL.jsx

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.jsx#L302

Added line #L302 was not covered by tests

const {
component: {
Expand Down Expand Up @@ -338,6 +339,13 @@ const AddressNLForm = ({initialValues, required, deriveAddress, layout, setFormi
return {message: ctx.defaultError}; // use global schema as fallback
};

const handleFormikAddressDirtyChange = newDirtyState => {

Check warning on line 342 in src/formio/components/AddressNL.jsx

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.jsx#L342

Added line #L342 was not covered by tests
// Only set the dirty state from `false` to `true`. Once it's dirty, it will remain dirty.
if (!dirty && newDirtyState) {
setDirty(newDirtyState);

Check warning on line 345 in src/formio/components/AddressNL.jsx

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.jsx#L345

Added line #L345 was not covered by tests
}
};

return (
<Formik
initialValues={initialValues}
Expand All @@ -347,35 +355,46 @@ const AddressNLForm = ({initialValues, required, deriveAddress, layout, setFormi
city: true,
}}
validateOnChange={false}
validationSchema={toFormikValidationSchema(
addressNLSchema(required, intl, {
postcode: {
pattern: postcodePattern,
errorMessage: postcodeError,
},
city: {
pattern: cityPattern,
errorMessage: cityError,
},
}),
{errorMap}
)}
validate={values =>

Check warning on line 358 in src/formio/components/AddressNL.jsx

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.jsx#L358

Added line #L358 was not covered by tests
dirty
? toFormikValidate(
addressNLSchema(required, intl, {
postcode: {
pattern: postcodePattern,
errorMessage: postcodeError,
},
city: {
pattern: cityPattern,
errorMessage: cityError,
},
}),
{errorMap}
)(values)
: {}
}
>
<FormikAddress
required={required}
setFormioValues={setFormioValues}
deriveAddress={deriveAddress}
layout={layout}
setDirty={handleFormikAddressDirtyChange}
/>
</Formik>
);
};

const FormikAddress = ({required, setFormioValues, deriveAddress, layout}) => {
const {values, isValid, setFieldValue} = useFormikContext();
const FormikAddress = ({required, setFormioValues, deriveAddress, layout, setDirty}) => {
const {values, isValid, setFieldValue, dirty} = useFormikContext();

Check warning on line 388 in src/formio/components/AddressNL.jsx

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.jsx#L388

Added line #L388 was not covered by tests
const {baseUrl} = useContext(ConfigContext);
const useColumns = layout === 'doubleColumn';

useEffect(() => {

Check warning on line 392 in src/formio/components/AddressNL.jsx

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.jsx#L392

Added line #L392 was not covered by tests
if (dirty) {
setDirty(dirty);

Check warning on line 394 in src/formio/components/AddressNL.jsx

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.jsx#L394

Added line #L394 was not covered by tests
}
}, [dirty, setDirty]);

useEffect(() => {
// *always* synchronize the state up, since:
//
Expand Down

0 comments on commit b16552c

Please sign in to comment.