Skip to content

Commit

Permalink
Refactor validation messages for improved readability and add visibil…
Browse files Browse the repository at this point in the history
…ity check for field validation
  • Loading branch information
simlarsen committed Nov 25, 2024
1 parent 1bc4f07 commit 450311d
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions Common/UI/Components/Forms/Validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@ export default class Validation {
if (content && field.validation) {
if (field.validation.minLength) {
if (content.trim().length < field.validation?.minLength) {
return `${field.title || name} cannot be less than ${
field.validation.minLength
} characters.`;
return `${field.title || name} cannot be less than ${field.validation.minLength
} characters.`;
}
}

if (field.validation.maxLength) {
if (content.trim().length > field.validation?.maxLength) {
return `${field.title || name} cannot be more than ${
field.validation.maxLength
} characters.`;
return `${field.title || name} cannot be more than ${field.validation.maxLength
} characters.`;
}
}

Expand Down Expand Up @@ -89,17 +87,15 @@ export default class Validation {

if (field.validation.maxValue) {
if (content > field.validation?.maxValue) {
return `${field.title || name} should not be more than ${
field.validation?.maxValue
}.`;
return `${field.title || name} should not be more than ${field.validation?.maxValue
}.`;
}
}

if (field.validation.minValue) {
if (content < field.validation?.minValue) {
return `${field.title || name} should not be less than ${
field.validation?.minValue
}.`;
return `${field.title || name} should not be less than ${field.validation?.minValue
}.`;
}
}
}
Expand Down Expand Up @@ -139,11 +135,10 @@ export default class Validation {
field.validation?.toMatchField &&
entity[field.validation?.toMatchField] &&
(entity[field.validation?.toMatchField] as string).toString().trim() !==
content.trim()
content.trim()
) {
return `${field.title} should match ${
field.validation?.toMatchField as string
}`;
return `${field.title} should match ${field.validation?.toMatchField as string
}`;
}
return null;
}
Expand Down Expand Up @@ -251,6 +246,17 @@ export default class Validation {

const name: string = field.name;


// is this field visible? If not visible, skip validation.

if (field.showIf) {
const isVisible: boolean = field?.showIf(args.values);

if(!isVisible) {
continue;
}
}

if (name in entries) {
const content: string | undefined = (entries as JSONObject)[
name
Expand Down

0 comments on commit 450311d

Please sign in to comment.