-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: validate page3 using unregister
The schema is cleaner if we register / unregister the necessary fields. This allows us to use a single top level undefined for a field that should not be validate instead of writing custom validation logic.
- Loading branch information
Showing
11 changed files
with
118 additions
and
224 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
apps/ui/components/application/ApplicationFormTextInput.tsx
This file contains 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,53 @@ | ||
import React from "react"; | ||
import { TextInput } from "hds-react"; | ||
import { useTranslation } from "next-i18next"; | ||
import { useFormContext } from "react-hook-form"; | ||
import { type ApplicationPage3FormValues } from "./form"; | ||
|
||
type TextFields = | ||
| "organisation.name" | ||
| "organisation.coreBusiness" | ||
| "organisation.identifier" | ||
| "organisation.address.streetAddress" | ||
| "organisation.address.postCode" | ||
| "organisation.address.city" | ||
| "contactPerson.firstName" | ||
| "contactPerson.lastName" | ||
| "contactPerson.phoneNumber" | ||
| "billingAddress.streetAddress" | ||
| "billingAddress.postCode" | ||
| "billingAddress.city" | ||
| "additionalInformation"; | ||
export function ApplicationFormTextInput({ | ||
name, | ||
disabled, | ||
}: { | ||
name: TextFields; | ||
disabled?: boolean; | ||
}): JSX.Element { | ||
const { t } = useTranslation(); | ||
const { register, getFieldState } = | ||
useFormContext<ApplicationPage3FormValues>(); | ||
|
||
const translateError = (errorMsg?: string) => | ||
errorMsg ? t(`application:validation.${errorMsg}`) : ""; | ||
const state = getFieldState(name); | ||
|
||
// avoid duplicating the translation key without polluting the props for a single case | ||
const transformedLabel = name.replace( | ||
"organisation.address.", | ||
"billingAddress." | ||
); | ||
|
||
return ( | ||
<TextInput | ||
{...register(name)} | ||
label={t(`application:Page3.${transformedLabel}`)} | ||
id={name} | ||
required={!disabled} | ||
disabled={disabled} | ||
invalid={!!state.error?.message} | ||
errorText={translateError(state.error?.message)} | ||
/> | ||
); | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,52 +1,19 @@ | ||
import React from "react"; | ||
import { useTranslation } from "next-i18next"; | ||
import { TextInput } from "hds-react"; | ||
import { useFormContext } from "react-hook-form"; | ||
import { FormSubHeading } from "./styled"; | ||
import { type ApplicationPage3FormValues } from "./form"; | ||
import { ApplicationFormTextInput } from "./ApplicationFormTextInput"; | ||
|
||
export function BillingAddress() { | ||
const { t } = useTranslation(); | ||
|
||
const { | ||
register, | ||
formState: { errors }, | ||
} = useFormContext<ApplicationPage3FormValues>(); | ||
|
||
const translateError = (errorMsg?: string) => | ||
errorMsg ? t(`application:validation.${errorMsg}`) : ""; | ||
|
||
return ( | ||
<> | ||
<FormSubHeading as="h2"> | ||
{t("application:Page3.subHeading.billingAddress")} | ||
</FormSubHeading> | ||
<TextInput | ||
{...register("billingAddress.streetAddress")} | ||
label={t("application:Page3.billingAddress.streetAddress")} | ||
id="billingAddress.streetAddress" | ||
required | ||
invalid={!!errors.billingAddress?.streetAddress?.message} | ||
errorText={translateError( | ||
errors.billingAddress?.streetAddress?.message | ||
)} | ||
/> | ||
<TextInput | ||
{...register("billingAddress.postCode")} | ||
label={t("application:Page3.billingAddress.postCode")} | ||
id="billingAddress.postCode" | ||
required | ||
invalid={!!errors.billingAddress?.postCode?.message} | ||
errorText={translateError(errors.billingAddress?.postCode?.message)} | ||
/> | ||
<TextInput | ||
{...register("billingAddress.city")} | ||
label={t("application:Page3.billingAddress.city")} | ||
id="billingAddress.city" | ||
required | ||
invalid={!!errors.billingAddress?.city?.message} | ||
errorText={translateError(errors.billingAddress?.city?.message)} | ||
/> | ||
<ApplicationFormTextInput name="billingAddress.streetAddress" /> | ||
<ApplicationFormTextInput name="billingAddress.postCode" /> | ||
<ApplicationFormTextInput name="billingAddress.city" /> | ||
</> | ||
); | ||
} |
This file contains 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 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 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 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
Oops, something went wrong.