mirror of
https://github.com/SuiteCRM/SuiteCRM-Core.git
synced 2025-08-29 07:50:08 +08:00
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:
parent
f315b3f839
commit
0916d14a5c
5 changed files with 53 additions and 16 deletions
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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')
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
@ -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')
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue