Skip to content

Commit

Permalink
Merge pull request #101 from maykinmedia/issue/intl-form-implementation
Browse files Browse the repository at this point in the history
Issue/intl form implementation
  • Loading branch information
Xaohs authored Jun 27, 2024
2 parents 62adfd9 + e485814 commit 28a193d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/components/form/form/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Attribute, AttributeData } from "../../../lib/data/attributedata";
import { FormField } from "../../../lib/form/typeguards";
import { getValueFromFormData, serializeForm } from "../../../lib/form/utils";
import {
DEFAULT_VALIDATION_ERROR_REQUIRED,
Validator,
getErrorFromErrors,
validateForm,
Expand Down Expand Up @@ -59,6 +60,9 @@ export type FormProps = Omit<
/** The submit form label. */
labelSubmit?: string;

/** The validation error label */
labelValidationErrorRequired?: string;

/**
* (Built-in serialization only), whether the convert results to type inferred from input type (e.g. checkbox value
* will be boolean).
Expand Down Expand Up @@ -98,6 +102,7 @@ export const Form: React.FC<FormProps> = ({
initialValues = {},
secondaryActions = [],
labelSubmit = "",
labelValidationErrorRequired = "",
nonFieldErrors,
onChange,
onSubmit,
Expand Down Expand Up @@ -129,6 +134,14 @@ export const Form: React.FC<FormProps> = ({
defaultMessage: "verzenden",
});

const _labelValidationErrorRequired = labelValidationErrorRequired
? labelValidationErrorRequired
: intl.formatMessage({
id: "mykn.components.Form.labelValidationErrorRequired",
description: 'mykn.components.Form: The "required" validation error',
defaultMessage: "Veld {name} is verplicht",
});

/**
* Revalidate on state change.
*/
Expand Down Expand Up @@ -205,11 +218,16 @@ export const Form: React.FC<FormProps> = ({
getValueFromFormData(fields, valuesState, field);

const error = getErrorFromErrors(fields, errorsState, field);
const message =
error === DEFAULT_VALIDATION_ERROR_REQUIRED
? _labelValidationErrorRequired
: error;

return (
<FormControl
key={field.id || index}
direction={direction}
error={error}
error={message}
forceShowError={!validateOnChange}
value={value}
onChange={defaultOnChange}
Expand Down
5 changes: 4 additions & 1 deletion src/lib/form/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { formatMessage } from "../i18n";
import { FormField } from "./typeguards";
import { parseFieldName } from "./utils";

export const DEFAULT_VALIDATION_ERROR_REQUIRED = 'Veld "{name}" is verplicht';

export type Validator = (
value: unknown,
field: FormField,
Expand All @@ -11,14 +13,15 @@ export type Validator = (

/**
* Validators whether `value` is set on `field` and `field` is required.
* If `message` is set, it will be used as error message, our `form` component has the option to set a custom message via props.
* @param value
* @param field
* @param message
*/
export const validateRequired: Validator = (
value,
field,
message = 'Field "{name}" is required',
message = DEFAULT_VALIDATION_ERROR_REQUIRED,
) => {
// Not required, don't validate.
if (!field.required || value) {
Expand Down
1 change: 1 addition & 0 deletions src/lib/i18n/compiled/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"mykn.components.DatePicker.labelWeek": "Week",
"mykn.components.DatePicker.labelWeekPrefix": "Week",
"mykn.components.Form.labelSubmit": "submit",
"mykn.components.Form.labelValidationErrorRequired": "Field {name} is required",
"mykn.components.Kanban.labelMoveObject": "change position of item",
"mykn.components.Kanban.labelSelectColumn": "move item to column",
"mykn.components.Logo.hrefLabel": "Go to \"{href}\"",
Expand Down
1 change: 1 addition & 0 deletions src/lib/i18n/compiled/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"mykn.components.DatePicker.labelWeek": "Week",
"mykn.components.DatePicker.labelWeekPrefix": "week",
"mykn.components.Form.labelSubmit": "verzenden",
"mykn.components.Form.labelValidationErrorRequired": "Veld {name} is verplicht",
"mykn.components.Kanban.labelMoveObject": "wijzig positie van onderdeel",
"mykn.components.Kanban.labelSelectColumn": "verplaats onderdeel naar kolom",
"mykn.components.Logo.hrefLabel": "Navigeer naar \"{href}\"",
Expand Down
5 changes: 5 additions & 0 deletions src/lib/i18n/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
"description": "mykn.components.Form: The submit form label",
"originalDefault": "verzenden"
},
"mykn.components.Form.labelValidationErrorRequired": {
"defaultMessage": "Field {name} is required",
"description": "mykn.components.Form: The \"required\" validation error",
"originalDefault": "Veld {name} is verplicht"
},
"mykn.components.Kanban.labelMoveObject": {
"defaultMessage": "change position of item",
"description": "mykn.components.Kanban: The kanban \"move object position\" (accessible) label",
Expand Down
5 changes: 5 additions & 0 deletions src/lib/i18n/messages/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
"description": "mykn.components.Form: The submit form label",
"originalDefault": "verzenden"
},
"mykn.components.Form.labelValidationErrorRequired": {
"defaultMessage": "Veld {name} is verplicht",
"description": "mykn.components.Form: The \"required\" validation error",
"originalDefault": "Veld {name} is verplicht"
},
"mykn.components.Kanban.labelMoveObject": {
"defaultMessage": "wijzig positie van onderdeel",
"description": "mykn.components.Kanban: The kanban \"move object position\" (accessible) label",
Expand Down

0 comments on commit 28a193d

Please sign in to comment.