diff --git a/.gitignore b/.gitignore index 259c76d7..d1460dc1 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,4 @@ Thumbs.db ./package-lock.json .turbo +.eslintcache diff --git a/projects/ngx-formentry/src/form-entry/form-factory/question.factory.ts b/projects/ngx-formentry/src/form-entry/form-factory/question.factory.ts index 88ddecad..9b5ec941 100644 --- a/projects/ngx-formentry/src/form-entry/form-factory/question.factory.ts +++ b/projects/ngx-formentry/src/form-entry/form-factory/question.factory.ts @@ -32,7 +32,7 @@ import { MaxLengthValidationModel } from '../question-models/max-length-validati import { MinLengthValidationModel } from '../question-models/min-length-validation.model'; import { WorkspaceLauncherQuestion } from '../question-models'; import { DecimalValidationModel } from '../question-models/decimal-validation.model'; - +import { DisallowDecimalsValidationModel } from '../question-models/disallow-decimals-validation.model'; @Injectable() export class QuestionFactory { dataSources: any = {}; @@ -1055,6 +1055,14 @@ export class QuestionFactory { }) ); } + if (questionOptions.disallowDecimals) { + validators.push( + new DisallowDecimalsValidationModel({ + type: 'disallowDecimals', + disallowDecimals: questionOptions.disallowDecimals + }) + ); + } break; default: diff --git a/projects/ngx-formentry/src/form-entry/form-factory/validation.factory.ts b/projects/ngx-formentry/src/form-entry/form-factory/validation.factory.ts index 19fa603d..3e1d8ad1 100644 --- a/projects/ngx-formentry/src/form-entry/form-factory/validation.factory.ts +++ b/projects/ngx-formentry/src/form-entry/form-factory/validation.factory.ts @@ -24,6 +24,7 @@ import { MaxLengthValidator } from '../validators/max-length.validator'; import { MaxLengthValidationModel } from '../question-models/max-length-validation.model'; import { MinLengthValidationModel } from '../question-models/min-length-validation.model'; import { MinLengthValidator } from '../validators/min-length.validator'; +import { DisallowDecimalsValidator } from '../validators/disallow-decimals.validator'; @Injectable() export class ValidationFactory { @@ -57,6 +58,9 @@ export class ValidationFactory { this.getMaxValueValidator((validator).max) ); break; + case 'disallowDecimals': + list.push(this.getDisallowedDecimalsValidator()); + break; case 'maxlength': list.push( this.maxLengthValidator( @@ -115,6 +119,10 @@ export class ValidationFactory { return new ConditionalAnsweredValidator(); } + get disallowDecimalsValidator(): DisallowDecimalsValidator { + return new DisallowDecimalsValidator(); + } + get requiredValidator(): any { return new RequiredValidator().validate; } @@ -151,6 +159,10 @@ export class ValidationFactory { return new MaxValidator().validate(max); } + public getDisallowedDecimalsValidator() { + return new DisallowDecimalsValidator().validate(); + } + get jsExpressionValidator() { return new JsExpressionValidator(); } @@ -170,6 +182,9 @@ export class ValidationFactory { case 'futureDateRestriction': messages.push(this.translate.instant('futureDateRestriction')); break; + case 'disallowDecimals': + messages.push(this.translate.instant('disallowDecimals')); + break; case 'minlength': messages.push( this.translate diff --git a/projects/ngx-formentry/src/form-entry/question-models/disallow-decimals-validation.model.ts b/projects/ngx-formentry/src/form-entry/question-models/disallow-decimals-validation.model.ts new file mode 100644 index 00000000..07a6d0cd --- /dev/null +++ b/projects/ngx-formentry/src/form-entry/question-models/disallow-decimals-validation.model.ts @@ -0,0 +1,25 @@ +import { ValidationModel } from './validation.model'; + +export class DisallowDecimalsValidationModel extends ValidationModel { + disallowDecimals: boolean; + failsWhenExpression: string; + message: string; + + constructor(validations: any) { + super(validations); + this.disallowDecimals = validations.disallowDecimals; + } + + setMessage(): void { + this.message = 'Decimal values are not allowed'; + } + + setFailsWhenExpression(): void { + this.failsWhenExpression = `!isEmpty(myValue) && (myValue).toString().includes('.')`; + } + + setValuesAndExpressions() { + this.setMessage(); + this.setFailsWhenExpression(); + } +} diff --git a/projects/ngx-formentry/src/form-entry/utils/messages.ts b/projects/ngx-formentry/src/form-entry/utils/messages.ts index 6a3de285..6d20eba1 100644 --- a/projects/ngx-formentry/src/form-entry/utils/messages.ts +++ b/projects/ngx-formentry/src/form-entry/utils/messages.ts @@ -24,4 +24,6 @@ export class Messages { public static readonly min = 'Please enter a value greater than or equal to {min}'; + + public static readonly disallowDecimals = 'Decimals values are not allowed'; } diff --git a/projects/ngx-formentry/src/form-entry/validators/disallow-decimals.validator.spec.ts b/projects/ngx-formentry/src/form-entry/validators/disallow-decimals.validator.spec.ts new file mode 100644 index 00000000..28951adf --- /dev/null +++ b/projects/ngx-formentry/src/form-entry/validators/disallow-decimals.validator.spec.ts @@ -0,0 +1,22 @@ +import { AfeFormControl } from '../../abstract-controls-extension/afe-form-control'; +import { DisallowDecimalsValidator } from './disallow-decimals.validator'; + +describe('DisallowDecimalsValidator', () => { + it('should return null when a non-decimal value is provided', () => { + const validator: DisallowDecimalsValidator = new DisallowDecimalsValidator(); + const number = '123'; + + const formControl = new AfeFormControl(number, [validator.validate]); + + expect(formControl.errors).toBe(null); + }); + + it('should return an error when a decimal value is provided', () => { + const validator: DisallowDecimalsValidator = new DisallowDecimalsValidator(); + const number = '123.456'; + + const formControl = new AfeFormControl(number, [validator.validate()]); + + expect(formControl.errors['disallowDecimals']).toBe(true); + }); +}); diff --git a/projects/ngx-formentry/src/form-entry/validators/disallow-decimals.validator.ts b/projects/ngx-formentry/src/form-entry/validators/disallow-decimals.validator.ts new file mode 100644 index 00000000..0fac6115 --- /dev/null +++ b/projects/ngx-formentry/src/form-entry/validators/disallow-decimals.validator.ts @@ -0,0 +1,18 @@ +import { AfeFormControl } from '../../abstract-controls-extension/afe-form-control'; + +export class DisallowDecimalsValidator { + validate() { + return (control: AfeFormControl): { [key: string]: any } => { + if (control.hidden) { + return null; + } + + if (control.value && control.value.length !== 0) { + const test: boolean = !/^\d+$/.test(control.value); + return test ? { disallowDecimals: true } : null; + } + + return null; + }; + } +} diff --git a/src/translations/en.json b/src/translations/en.json index 6c3af6ac..8b3a6d00 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -13,9 +13,10 @@ "changeToMonthView": "Change to month view", "chooseMonthAndYear": "Choose month and year", "clearEntry": "Are you sure you want to clear this entry?", - "componentLoadingFailed": "Component Loading failed...", + "componentLoadingFailed": "Component loading failed...", "daysAgo": " days ago", "deleteEntry": "Are you sure you want to delete this item?", + "disallowDecimals": "Decimal values are not allowed", "enterMoreCharacters": "Please enter 2 or more characters", "fix": "Fix", "from": "From",