Skip to content

Commit 4aaac44

Browse files
committed
fix(#2403): fix DRep link and description validation
1 parent 840e065 commit 4aaac44

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ changes.
1717
### Fixed
1818

1919
- Fix calculating DRep live voting power [Issue 2460](https://github.com/IntersectMBO/govtool/issues/2460)
20+
- Fix link and description validation [Issue 2403](https://github.com/IntersectMBO/govtool/issues/2403)
2021

2122
### Changed
2223

govtool/frontend/src/components/molecules/DRepDataForm.tsx

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
Control,
44
FieldErrors,
55
UseFormRegister,
6+
UseFormWatch,
67
useFieldArray,
78
} from "react-hook-form";
89
import { Box } from "@mui/material";
@@ -20,9 +21,10 @@ type Props = {
2021
control: Control<DRepDataFormValues>;
2122
errors: FieldErrors<DRepDataFormValues>;
2223
register: UseFormRegister<DRepDataFormValues>;
24+
watch: UseFormWatch<DRepDataFormValues>;
2325
};
2426

25-
export const DRepDataForm = ({ control, errors, register }: Props) => {
27+
export const DRepDataForm = ({ control, errors, register, watch }: Props) => {
2628
const { t } = useTranslation();
2729
const { isMobile } = useScreenDimension();
2830

@@ -111,12 +113,14 @@ export const DRepDataForm = ({ control, errors, register }: Props) => {
111113
control={control}
112114
errors={errors}
113115
register={register}
116+
watch={watch}
114117
/>
115118
<ReferencesSection
116119
type="identity"
117120
control={control}
118121
errors={errors}
119122
register={register}
123+
watch={watch}
120124
/>
121125
</Box>
122126
<div>
@@ -176,13 +180,15 @@ type ReferencesSectionProps = {
176180
control: Control<DRepDataFormValues>;
177181
errors: FieldErrors<DRepDataFormValues>;
178182
register: UseFormRegister<DRepDataFormValues>;
183+
watch: UseFormWatch<DRepDataFormValues>;
179184
};
180185

181186
const ReferencesSection = ({
182187
type,
183188
control,
184189
errors,
185190
register,
191+
watch,
186192
}: ReferencesSectionProps) => {
187193
const { fieldName, jsonldType } = (() => {
188194
// eslint-disable-next-line default-case
@@ -237,7 +243,16 @@ const ReferencesSection = ({
237243
helpfulTextDataTestId={`${type}-reference-description-${
238244
index + 1
239245
}-hint`}
240-
rules={Rules.LINK_DESCRIPTION}
246+
rules={{
247+
...Rules.LINK_DESCRIPTION,
248+
validate: (value) => {
249+
const isLink = watch(`${fieldName}.${index}.uri`);
250+
if (!value && Boolean(isLink)) {
251+
return t("dRepData.required");
252+
}
253+
return true;
254+
},
255+
}}
241256
/>
242257
<ControlledField.Input
243258
{...register(`${fieldName}.${index}.uri`)}
@@ -258,7 +273,16 @@ const ReferencesSection = ({
258273
dataTestId={`${type}-reference-url-${index + 1}-input`}
259274
errorDataTestId={`${type}-reference-url-${index + 1}-error`}
260275
helpfulTextDataTestId={`${type}-reference-url-${index + 1}-hint`}
261-
rules={Rules.LINK_URL}
276+
rules={{
277+
...Rules.LINK_URL,
278+
validate: (value) => {
279+
const isDescription = watch(`${fieldName}.${index}.label`);
280+
if (!value && Boolean(isDescription)) {
281+
return t("dRepData.required");
282+
}
283+
return true;
284+
},
285+
}}
262286
/>
263287
</Fragment>
264288
))}

govtool/frontend/src/components/organisms/EditDRepInfoSteps/EditDRepForm.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ export const EditDRepForm = ({
6767

6868
return (
6969
<Box sx={{ display: "flex", flexDirection: "column", gap: 8 }}>
70-
<DRepDataForm control={control} errors={errors} register={register} />
70+
<DRepDataForm
71+
control={control}
72+
errors={errors}
73+
register={register}
74+
watch={watch}
75+
/>
7176
<CenteredBoxBottomButtons
7277
onActionButton={onClickContinue}
7378
disableActionButton={isContinueButtonDisabled}

govtool/frontend/src/components/organisms/RegisterAsDRepSteps/RegisterAsDRepForm.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ export const RegisterAsDRepForm = ({
2525

2626
return (
2727
<Box sx={{ display: "flex", flexDirection: "column", gap: 8 }}>
28-
<DRepDataForm control={control} errors={errors} register={register} />
28+
<DRepDataForm
29+
control={control}
30+
errors={errors}
31+
register={register}
32+
watch={watch}
33+
/>
2934
<CenteredBoxBottomButtons
3035
onActionButton={onClickContinue}
3136
disableActionButton={isContinueButtonDisabled}

0 commit comments

Comments
 (0)