diff --git a/src/pages/organisationUnits/New.spec.tsx b/src/pages/organisationUnits/New.spec.tsx index 08135911..3a780888 100644 --- a/src/pages/organisationUnits/New.spec.tsx +++ b/src/pages/organisationUnits/New.spec.tsx @@ -28,4 +28,6 @@ describe('Indicator type add form additional tests', () => { it('should show an error if latitude is above 180', () => {}) it('should show an error if latitude is below -180', () => {}) it('should show an error if email is not valid', () => {}) + it('should show an error if parent org unit is not selected', () => {}) + it('should allow creation of the first org unit', () => {}) }) diff --git a/src/pages/organisationUnits/form/OrganisationUnitSelector.tsx b/src/pages/organisationUnits/form/OrganisationUnitSelector.tsx index b66a7449..f12dd070 100644 --- a/src/pages/organisationUnits/form/OrganisationUnitSelector.tsx +++ b/src/pages/organisationUnits/form/OrganisationUnitSelector.tsx @@ -2,13 +2,17 @@ import i18n from '@dhis2/d2-i18n' import { Field, NoticeBox, OrganisationUnitTree } from '@dhis2/ui' import { IconInfo16 } from '@dhis2/ui-icons' import React, { useState } from 'react' -import { useField } from 'react-final-form' +import { FieldRenderProps, useField } from 'react-final-form' import { useCurrentUserRootOrgUnits } from '../../../lib/user/currentUserStore' import classes from './OrganisationUnitSelector.module.css' export function OrganisationUnitSelector() { const fieldName = 'parent' - const { input, meta } = useField(fieldName, { format: (value) => value }) + const { input, meta } = useField(fieldName, { + format: (value) => value, + validate: (value) => + !value && userRootOrgUnits.length > 0 ? 'Required' : undefined, + }) const userRootOrgUnits = useCurrentUserRootOrgUnits() const userRootOrgUnitsIds = userRootOrgUnits.map((unit) => unit.id) const userRootOrgUnitsPaths = userRootOrgUnits.map((unit) => unit.path)