Skip to content

Commit

Permalink
Bedre feilmelding på max-lengde (#494)
Browse files Browse the repository at this point in the history
  • Loading branch information
tu55eladd authored Sep 28, 2023
1 parent 811fe1e commit 496d06b
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/view/dialog/henvendelseInput/MeldingInputBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { zodResolver } from '@hookform/resolvers/zod';
import { Alert, Button, ErrorMessage } from '@navikt/ds-react';
import classNames from 'classnames';
import React, { ChangeEvent, useEffect, useLayoutEffect, useRef, useState } from 'react';
import { useForm } from 'react-hook-form';
import { FieldError, useForm } from 'react-hook-form';
import { z } from 'zod';

import loggEvent from '../../../felleskomponenter/logging';
Expand Down Expand Up @@ -93,7 +93,8 @@ const MeldingInputBox = (props: Props) => {
register,
handleSubmit,
reset,
formState: { errors, isSubmitting }
formState: { errors, isSubmitting },
getValues
} = useForm<MeldingFormValues>({
defaultValues,
resolver: zodResolver(schema)
Expand Down Expand Up @@ -125,6 +126,8 @@ const MeldingInputBox = (props: Props) => {
});
};

console.log(errors);

return (
<form
className={classNames({
Expand Down Expand Up @@ -182,7 +185,7 @@ const MeldingInputBox = (props: Props) => {
) : null}
{errors.melding ? (
<ErrorMessage className="mt-2" size="small">
{errors.melding.message}
{betterErrorMessage(errors.melding, getValues('melding')).message}
</ErrorMessage>
) : null}
{noeFeilet ? (
Expand All @@ -194,4 +197,14 @@ const MeldingInputBox = (props: Props) => {
);
};

const betterErrorMessage = (error: FieldError, melding: string): FieldError => {
const tooBig = error.type === 'too_big';
return {
...error,
message: tooBig
? `Meldingen kan ikke være mer enn ${maxMeldingsLengde} tegn, men er ${melding.length}`
: error.message
};
};

export default MeldingInputBox;

0 comments on commit 496d06b

Please sign in to comment.