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

FSADT1-1147: Fixed validation to allow US zip code format #743

Merged
merged 1 commit into from
Jan 17, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class ClientAddressDtoValidator implements Validator {
private final ProvinceCodeRepository provinceCodeRepository;

private static final Pattern CA_POSTAL_CODE_FORMAT = Pattern.compile("[A-Z]\\d[A-Z]\\d[A-Z]\\d");
private static final Pattern US_ZIP_CODE_FORMAT = Pattern.compile("^\\d{5}(?:-\\d{4})?$");

@Override
public boolean supports(Class<?> clazz) {
Expand Down Expand Up @@ -113,7 +114,7 @@ private void handleBlankPostalCode(String postalCode, String country, String pos
errors.rejectValue(postalCodeField, "You must include a postal code in the format A9A9A9.");
}
else if ("US".equalsIgnoreCase(country)) {
errors.rejectValue(postalCodeField, "You must include a ZIP code in the format 00000.");
errors.rejectValue(postalCodeField, "You must include a ZIP code in the format 000000 or 00000-0000.");
}
else {
errors.rejectValue(postalCodeField, "You must include a postal code.");
Expand All @@ -134,13 +135,13 @@ private void handlePostalCodeLengthAndFormat(String postalCode, String country,
}
}
else if ("US".equalsIgnoreCase(country)) {
// For US, postal code should be digits (numbers only)
if (!StringUtils.isNumeric(postalCode)) {
errors.rejectValue(postalCodeField, "should be numeric");
// For US, postal code should be up to 10 digits
if (StringUtils.length(postalCode) > 10) {
errors.rejectValue(postalCodeField, "has more than 10 characters");
}
// For US, postal code should be up to 5 digits
if (StringUtils.length(postalCode) > 5) {
errors.rejectValue(postalCodeField, "has more than 5 characters");
// CA postal code format is A9A9A9
if (!US_ZIP_CODE_FORMAT.matcher(postalCode).matches()) {
errors.rejectValue(postalCodeField, "invalid US zip code format");
}
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ const goToStep = (step: number) => {
kind="error"
title="Your application could not be submitted:"
>
<div>{{ globalErrorMessage.errorMsg }}</div>
<div>{{ globalErrorMessage.fieldName }} {{ globalErrorMessage.errorMsg }}</div>
</cds-actionable-notification>

<cds-actionable-notification
Expand Down
1 change: 1 addition & 0 deletions frontend/src/dto/CommonTypesDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface BusinessSearchResult {

export interface ValidationMessageType {
fieldId: string;
fieldName: string;
errorMsg: string;
originalValue?: string;
}
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/pages/FormBCSCPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { isTouchScreen } from "@/composables/useScreenSize";
import { useEventBus } from "@vueuse/core";
import {
codeConversionFn,
convertFieldNameToSentence,
getEnumKeyByEnumValue,
} from "@/services/ForestClientService";
import {
Expand Down Expand Up @@ -325,6 +326,7 @@ watch([error], () => {
validationErrors.forEach((errorItem: ValidationMessageType) =>
notificationBus.emit({
fieldId: "server.validation.error",
fieldName: convertFieldNameToSentence(errorItem.fieldId),
errorMsg: errorItem.errorMsg,
})
);
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/pages/FormBCeIDPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import ArrowRight16 from "@carbon/icons-vue/es/arrow--right/16";
import LogOut16 from "@carbon/icons-vue/es/logout/16";
// @ts-ignore
import Check16 from "@carbon/icons-vue/es/checkmark/16";
import { convertFieldNameToSentence } from "@/services/ForestClientService";

const errorBus = useEventBus<ValidationMessageType[]>(
"submission-error-notification"
Expand Down Expand Up @@ -127,6 +128,7 @@ watch([error], () => {
validationErrors.forEach((errorItem: ValidationMessageType) =>
notificationBus.emit({
fieldId: "server.validation.error",
fieldName: convertFieldNameToSentence(errorItem.fieldId),
errorMsg: errorItem.errorMsg,
})
);
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/services/ForestClientService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,13 @@ export const openMailtoLink = (email) => {
const mailtoLink = 'mailto:' + encodedEmail;
location.assign(mailtoLink);
}

export const convertFieldNameToSentence = (input: string): string => {
const lastPart = input.split('.').pop();

const words = lastPart
.replace(/([a-z])([A-Z])/g, '$1 $2') // Insert space between camel case
.split(/\s+/);

return words.map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(' ');
};
Loading