Fix front end type validators

- Add missing empty check to type validators
-- type validators should not apply if empty
- Fix toInternal conversion
This commit is contained in:
Clemente Raposo 2021-01-02 20:30:32 +00:00 committed by Dillon-Brown
parent f315b3f839
commit 0916d14a5c
5 changed files with 53 additions and 16 deletions

View file

@ -3,6 +3,7 @@ import {UserPreferenceStore} from '@store/user-preference/user-preference.store'
import {formatCurrency, formatNumber} from '@angular/common';
import {NumberFormatter} from '@services/formatters/number/number-formatter.service';
import {FormatOptions, Formatter} from '@services/formatters/formatter.model';
import {isVoid} from '@app-common/utils/value-utils';
export interface CurrencyFormat {
iso4217: string;
@ -24,6 +25,10 @@ export class CurrencyFormatter implements Formatter {
toUserFormat(value: string, options: FormatOptions = null): string {
if (isVoid(value) || value === '') {
return '';
}
const symbol = (options && options.symbol) || this.getSymbol();
const code = (options && options.code) || this.getCode();
let digits = null;

View file

@ -2,6 +2,7 @@ import {Inject, Injectable, LOCALE_ID} from '@angular/core';
import {UserPreferenceStore} from '@store/user-preference/user-preference.store';
import {formatNumber} from '@angular/common';
import {Formatter} from '@services/formatters/formatter.model';
import {isVoid} from '@app-common/utils/value-utils';
@Injectable({
providedIn: 'root'
@ -15,25 +16,42 @@ export class NumberFormatter implements Formatter {
}
toUserFormat(value: string): string {
if (isVoid(value) || value === '') {
return '';
}
const formatted = formatNumber(Number(value), this.locale);
return this.replaceSeparators(formatted);
}
toInternalFormat(value: string): string {
const group = this.getGroupSymbol();
const decimals = this.getDecimalsSymbol();
const pattern = this.getFloatUserFormatPattern();
const regex = new RegExp(pattern);
if (!regex.test(value)) {
return '';
if (!value) {
return value;
}
let transformed = value.replace(group, '');
transformed = transformed.replace(decimals, '.');
const decimalSymbol = this.getDecimalsSymbol() || '.';
const groupSymbol = this.getGroupSymbol() || ',';
return transformed;
let decimalSymbolRegex = new RegExp(decimalSymbol, 'g');
if (decimalSymbol === '.') {
decimalSymbolRegex = new RegExp('\\.', 'g');
}
let groupSymbolRegex = new RegExp(groupSymbol, 'g');
if (groupSymbol === '.') {
groupSymbolRegex = new RegExp('\\.', 'g');
}
value = value.replace(groupSymbolRegex, 'group_separator');
value = value.replace(decimalSymbolRegex, 'decimal_separator');
value = value.replace(/decimal_separator/g, '.');
value = value.replace(/group_separator/g, '');
return value;
}
getFloatUserFormatPattern(): string {
@ -89,14 +107,14 @@ export class NumberFormatter implements Formatter {
return transformed;
}
transformed = transformed.replace(',', 'group_separator');
transformed = transformed.replace('.', 'decimal_separator');
transformed = transformed.replace(/,/g, 'group_separator');
transformed = transformed.replace(/\./g, 'decimal_separator');
const decimalSymbol = this.getDecimalsSymbol() || '.';
const groupSymbol = this.getGroupSymbol() || ',';
transformed = transformed.replace('decimal_separator', decimalSymbol);
transformed = transformed.replace('group_separator', groupSymbol);
transformed = transformed.replace(/decimal_separator/g, decimalSymbol);
transformed = transformed.replace(/group_separator/g, groupSymbol);
return transformed;
}

View file

@ -8,6 +8,11 @@ import {StandardValidationErrors, StandardValidatorFn} from '@app-common/service
export const dateValidator = (formatter: DateFormatter): StandardValidatorFn => (
(control: AbstractControl): StandardValidationErrors | null => {
if (control.value == null || control.value.length === 0) {
return null;
}
const invalid = formatter.validateUserFormat(control.value);
return invalid ? {
invalidDate: {
@ -16,7 +21,7 @@ export const dateValidator = (formatter: DateFormatter): StandardValidatorFn =>
labelKey: 'LBL_VALIDATION_ERROR_DATE_FORMAT',
context: {
value: control.value,
expected: formatter.toUserFormat('2020-01-12')
expected: formatter.toUserFormat('2020-01-23')
}
}
},

View file

@ -8,6 +8,11 @@ import {StandardValidationErrors, StandardValidatorFn} from '@app-common/service
export const dateTimeValidator = (formatter: DatetimeFormatter): StandardValidatorFn => (
(control: AbstractControl): StandardValidationErrors | null => {
if (control.value == null || control.value.length === 0) {
return null;
}
const invalid = formatter.validateUserFormat(control.value);
return invalid ? {
dateTimeValidator: {
@ -16,7 +21,7 @@ export const dateTimeValidator = (formatter: DatetimeFormatter): StandardValidat
labelKey: 'LBL_VALIDATION_ERROR_DATETIME_FORMAT',
context: {
value: control.value,
expected: formatter.toUserFormat('2020-01-12 12:30:40')
expected: formatter.toUserFormat('2020-01-23 12:30:40')
}
}
},

View file

@ -8,6 +8,10 @@ import {StandardValidationErrors, StandardValidatorFn} from '@app-common/service
export const emailValidator = (): StandardValidatorFn => (
(control: AbstractControl): StandardValidationErrors | null => {
if (control.value == null || control.value.length === 0) {
return null;
}
const result = Validators.email(control);
if (result === null) {