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

Issue/intl form implementation #101

Merged
merged 4 commits into from
Jun 27, 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
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