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

fix: [DHIS2-18676] display error for age in the future #3932

Open
wants to merge 1 commit into
base: DHIS2-18310-enable-non-gregorian-calendars-in-views-and-lists
Choose a base branch
from
Open
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
@@ -1,20 +1,23 @@
// @flow
import moment from 'moment';
import { orientations } from '../../../../FormFields/New';
import { createFieldConfig, createProps } from '../base/configBaseDefaultForm';
import { AgeFieldForForm } from '../../Components';
import { type DataElement } from '../../../../../metaData';
import { convertDateObjectToDateFormatString } from '../../../../../../capture-core/utils/converters/date';
import { type DateDataElement } from '../../../../../metaData';
import type { QuerySingleResource } from '../../../../../utils/api/api.types';

const getCalendarAnchorPosition = (formHorizontal: ?boolean) => (formHorizontal ? 'center' : 'left');

export const getAgeFieldConfig = (metaData: DataElement, options: Object, querySingleResource: QuerySingleResource) => {
export const getAgeFieldConfig = (metaData: DateDataElement, options: Object, querySingleResource: QuerySingleResource) => {
const props = createProps({
formHorizontal: options.formHorizontal,
fieldLabelMediaBasedClass: options.fieldLabelMediaBasedClass,
orientation: options.formHorizontal ? orientations.VERTICAL : orientations.HORIZONTAL,
shrinkDisabled: options.formHorizontal,
dateCalendarWidth: options.formHorizontal ? 250 : 350,
datePopupAnchorPosition: getCalendarAnchorPosition(options.formHorizontal),
calendarMax: !metaData.allowFutureDate ? convertDateObjectToDateFormatString(moment()) : undefined,
}, options, metaData);

return createFieldConfig({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ const fieldForTypes: FieldForTypes = {
[dataElementTypes.TIME_RANGE]: getTextRangeFieldConfig,
[dataElementTypes.PERCENTAGE]: getTextFieldConfig,
[dataElementTypes.URL]: getTextFieldConfig,
[dataElementTypes.AGE]: getAgeFieldConfig,
[dataElementTypes.AGE]: (metaData: any, options: Object, querySingleResource: QuerySingleResource) =>
getAgeFieldConfig(metaData, options, querySingleResource),
[dataElementTypes.ORGANISATION_UNIT]: getOrgUnitFieldConfig,
[dataElementTypes.COORDINATE]: getCoordinateFieldConfig,
[dataElementTypes.POLYGON]: getPolygonFieldConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from 'capture-core-utils/validators/form';
import {
isValidAge,
isValidNonFutureAge,
isValidDate,
isValidNonFutureDate,
isValidDateTime,
Expand Down Expand Up @@ -153,6 +154,11 @@ const validatorsForTypes = {
validator: isValidAge,
message: errorMessages.AGE,
type: validatorTypes.TYPE_BASE,
},
{
validator: isValidNonFutureAge,
type: validatorTypes.TYPE_EXTENDED,
message: errorMessages.DATE_FUTURE_NOT_ALLOWED,
}],
[dataElementTypes.PHONE_NUMBER]: [{
validator: isValidPhoneNumber,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,21 @@ export function isValidAge(value: Object, internalComponentError?: ?{error: ?str
return false;
}

const numberResult = validateNumbers(
if (internalComponentError && internalComponentError?.errorCode === 'INVALID_DATE_MORE_THAN_MAX') {
return { valid: true, errorMessage: null };
}

const dateValidation = value.date
? validateDate(value.date, internalComponentError)
: { valid: true, errorMessage: null };

if (!dateValidation.valid) {
return dateValidation;
}

return validateNumbers(
value.years,
value.months,
value.days,
);

if (!numberResult.valid) return numberResult;

return validateDate(value.date, internalComponentError);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export { getDateRangeValidator } from './getDateRangeValidator';
export { getDateTimeRangeValidator } from './getDateTimeRangeValidator';
export { getNumberRangeValidator } from './getNumberRangeValidator';
export { getTimeRangeValidator } from './getTimeRangeValidator';
export { isValidNonFutureAge } from './isValidNonFutureAge';
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @flow
import i18n from '@dhis2/d2-i18n';

const CUSTOM_VALIDATION_MESSAGES = {
INVALID_DATE_MORE_THAN_MAX: i18n.t('A date in the future is not allowed'),
};

export const isValidNonFutureAge = (value: string, internalComponentError?: ?{error: ?string, errorCode: ?string}) => {
if (!value) {
return true;
}

if (internalComponentError && internalComponentError?.errorCode === 'INVALID_DATE_MORE_THAN_MAX') {
return {
valid: false,
errorMessage: { date: CUSTOM_VALIDATION_MESSAGES.INVALID_DATE_MORE_THAN_MAX },
};
}

return true;
};
1 change: 1 addition & 0 deletions src/core_modules/capture-ui/AgeField/ageField.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
}

.ageDateInputContainerHorizontal {
width: 150px;
}

.ageClearHorizontal {
Expand Down
Loading