Merge pull request #104 from sartography/feature/date_form_validations
Feature/date form validations
This commit is contained in:
commit
994620bc5f
|
@ -138,6 +138,34 @@ export default function TaskShow() {
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getFieldsWithDateValidations = (
|
||||||
|
jsonSchema: any,
|
||||||
|
formData: any,
|
||||||
|
errors: any
|
||||||
|
) => {
|
||||||
|
if ('properties' in jsonSchema) {
|
||||||
|
Object.keys(jsonSchema.properties).forEach((propertyKey: string) => {
|
||||||
|
const propertyMetadata = jsonSchema.properties[propertyKey];
|
||||||
|
if (
|
||||||
|
'minimumDate' in propertyMetadata &&
|
||||||
|
propertyMetadata.minimumDate === 'today'
|
||||||
|
) {
|
||||||
|
const dateToday = new Date();
|
||||||
|
const dateValue = formData[propertyKey];
|
||||||
|
if (dateValue) {
|
||||||
|
const dateValueObject = new Date(dateValue);
|
||||||
|
const dateValueString = dateValueObject.toISOString().split('T')[0];
|
||||||
|
const dateTodayString = dateToday.toISOString().split('T')[0];
|
||||||
|
if (dateTodayString > dateValueString) {
|
||||||
|
errors[propertyKey].addError('must be today or after');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return errors;
|
||||||
|
};
|
||||||
|
|
||||||
const formElement = (taskToUse: any) => {
|
const formElement = (taskToUse: any) => {
|
||||||
let formUiSchema;
|
let formUiSchema;
|
||||||
let taskData = taskToUse.data;
|
let taskData = taskToUse.data;
|
||||||
|
@ -184,12 +212,9 @@ export default function TaskShow() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function customValidate(formData: any, errors: any) {
|
const customValidate = (formData: any, errors: any) => {
|
||||||
if (formData.pass1 !== formData.pass2) {
|
return getFieldsWithDateValidations(jsonSchema, formData, errors);
|
||||||
errors.pass2.addError("Passwords don't match");
|
};
|
||||||
}
|
|
||||||
return errors;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Grid fullWidth condensed>
|
<Grid fullWidth condensed>
|
||||||
|
|
Loading…
Reference in New Issue