Skip to content

Commit

Permalink
✨ [open-formulieren/open-forms#4420] Validation for AddressNL fields
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Jul 9, 2024
1 parent 4d03133 commit 9105878
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/formio/components/AddressNL.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export default class AddressNL extends Field {
value={{
baseUrl: this.options.baseUrl,
requiredFieldsWithAsterisk: this.options.evalContext.requiredFieldsWithAsterisk,
component: this.component,
}}
>
<AddressNLForm
Expand Down Expand Up @@ -386,10 +387,15 @@ const FormikAddress = ({required, setFormioValues, deriveAddress, layout}) => {
};

const PostCodeField = ({required, autoFillAddress}) => {
const intl = useIntl();

Check warning on line 390 in src/formio/components/AddressNL.js

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.js#L390

Added line #L390 was not covered by tests
const {values, setFieldValue, getFieldProps} = useFormikContext();
const {onBlur: onBlurFormik} = getFieldProps('postcode');
const postcode = values['postcode'];

const {component} = useContext(ConfigContext);

Check warning on line 395 in src/formio/components/AddressNL.js

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.js#L395

Added line #L395 was not covered by tests

const postcodePattern = new RegExp(component.components.postcode.validate.pattern);

Check warning on line 397 in src/formio/components/AddressNL.js

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.js#L397

Added line #L397 was not covered by tests

const onBlur = event => {
onBlurFormik(event);
// format the postcode with a space in between
Expand All @@ -402,13 +408,22 @@ const PostCodeField = ({required, autoFillAddress}) => {
autoFillAddress();
};

const validate = data => {

Check warning on line 411 in src/formio/components/AddressNL.js

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.js#L411

Added line #L411 was not covered by tests
let error;
if (!postcodePattern.test(data)) {
error = component.components.postcode.translatedErrors[intl.locale].pattern;

Check warning on line 414 in src/formio/components/AddressNL.js

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.js#L414

Added line #L414 was not covered by tests
}
return error;

Check warning on line 416 in src/formio/components/AddressNL.js

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.js#L416

Added line #L416 was not covered by tests
};

return (
<TextField
name="postcode"
label={<FormattedMessage {...FIELD_LABELS.postcode} />}
placeholder="1234 AB"
isRequired={required}
onBlur={onBlur}
validate={validate}
/>
);
};
Expand Down
10 changes: 10 additions & 0 deletions src/formio/components/AddressNL.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ export default {
validate: {
required: true,
},
components: {
postcode: {
validate: {pattern: '1015 [a-zA-Z]{2}'},
translatedErrors: {
nl: {
pattern: 'De postcode moet beginnen met 1015',
},
},
},
},
},
evalContext: {},
},
Expand Down

0 comments on commit 9105878

Please sign in to comment.