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

feat: Adds question label to error message #3963

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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 @@ -4,6 +4,7 @@ import React from "react";
export interface ErrorListProps {
errorKey: string;
value: string | undefined;
label?: string;
}

/**
Expand All @@ -23,9 +24,10 @@ const scrollErrorInView = (id: string) => {
* @returns ReactElement for displaying the error list
* @param errorKey The key for the form element for the error
* @param value The error to be displayed
* @param label The label for the form element
*/
export const ErrorListItem = (props: ErrorListProps): React.ReactElement => {
const { errorKey, value } = props;
const { errorKey, value, label } = props;

return (
<li>
Expand All @@ -43,6 +45,7 @@ export const ErrorListItem = (props: ErrorListProps): React.ReactElement => {
scrollErrorInView(errorKey);
}}
>
{label && `${label} - `}
{value}
</a>
</li>
Expand Down
19 changes: 18 additions & 1 deletion lib/validation/validation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export const validateOnSubmit = (
*/

export const getErrorList = (
props: { formRecord: PublicFormRecord } & FormikProps<Responses>
props: { formRecord: PublicFormRecord; language: string } & FormikProps<Responses>
): JSX.Element | null => {
if (!props.formRecord?.form || !props.errors) {
return null;
Expand All @@ -273,6 +273,19 @@ export const getErrorList = (
return [element, props.errors[element]];
});

// TODO may want to truncate long labels with css instead
// TODO test dynamic rows
const getLabel = (elementKey: string | number | undefined): string => {
const formElement = props.formRecord.form.elements.filter(
(element) => element.id === elementKey
)[0];
const formLabel =
(props.language === "fr"
? formElement?.properties?.titleFr
: formElement?.properties?.titleEn) || "";
return formLabel.length > 24 ? formLabel.substring(0, 24) + "..." : formLabel;
};

if (props.touched && sortedFormElementErrors.length) {
errorList = sortedFormElementErrors.map(([formElementKey, formElementErrorValue]) => {
if (Array.isArray(formElementErrorValue)) {
Expand All @@ -285,6 +298,9 @@ export const getErrorList = (
key={`error-${formElementKey}.${dynamicRowIndex}.${dyanamicRowElementKey}`}
errorKey={`${formElementKey}.${dynamicRowIndex}.${dyanamicRowElementKey}`}
value={`${dyanamicRowElementErrorValue as string}`}
label={getLabel(
`${formElementKey}.${dynamicRowIndex}.${dyanamicRowElementKey}`
)}
/>
)
);
Expand All @@ -297,6 +313,7 @@ export const getErrorList = (
key={`error-${formElementKey}`}
errorKey={`${formElementKey}`}
value={`${formElementErrorValue}`}
label={getLabel(formElementKey)}
/>
);
}
Expand Down
Loading