mirror of
https://github.com/SuiteCRM/SuiteCRM-Core.git
synced 2025-08-29 11:00:40 +08:00
Fix field mapping to internal format
- Use user format when building field formControl - Fix phone formatter regex - Add edit mode option to currency user formatting - Cleanup date and datetime formatters - Fix toInternalFormat processing
This commit is contained in:
parent
bb8d75a38b
commit
ba542821c2
8 changed files with 81 additions and 39 deletions
|
@ -14,7 +14,7 @@ export class DatetimeParserFormatter extends NgbDateParserFormatter {
|
|||
return null;
|
||||
}
|
||||
|
||||
return this.formatter.fromUserDate(value);
|
||||
return this.formatter.userFormatToStruct(value);
|
||||
}
|
||||
|
||||
format(date: NgbDateStruct | null): string {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import {Inject, Injectable, LOCALE_ID} from '@angular/core';
|
||||
import {UserPreferenceStore} from '@store/user-preference/user-preference.store';
|
||||
import {formatCurrency} from '@angular/common';
|
||||
import {formatCurrency, formatNumber} from '@angular/common';
|
||||
import {NumberFormatter} from '@services/formatters/number/number-formatter.service';
|
||||
import {FormatOptions, Formatter} from '@services/formatters/formatter.model';
|
||||
|
||||
|
@ -23,6 +23,7 @@ export class CurrencyFormatter implements Formatter {
|
|||
}
|
||||
|
||||
toUserFormat(value: string, options: FormatOptions = null): string {
|
||||
|
||||
const symbol = (options && options.symbol) || this.getSymbol();
|
||||
const code = (options && options.code) || this.getCode();
|
||||
let digits = null;
|
||||
|
@ -31,8 +32,14 @@ export class CurrencyFormatter implements Formatter {
|
|||
}
|
||||
|
||||
const digitsInfo = this.getDigitsInfo(digits);
|
||||
let formatted: string;
|
||||
|
||||
const formatted = formatCurrency(Number(value), this.locale, symbol, code, digitsInfo);
|
||||
if (options && options.mode === 'edit') {
|
||||
formatted = formatNumber(Number(value), this.locale, digitsInfo);
|
||||
return this.replaceSeparators(formatted);
|
||||
}
|
||||
|
||||
formatted = formatCurrency(Number(value), this.locale, symbol, code, digitsInfo);
|
||||
return this.replaceSeparators(formatted);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
import {Injectable} from '@angular/core';
|
||||
import {formatDate} from '@angular/common';
|
||||
import {DateTime} from 'luxon';
|
||||
import {DatetimeFormatter} from '@services/formatters/datetime/datetime-formatter.service';
|
||||
|
||||
@Injectable({
|
||||
|
@ -8,26 +6,11 @@ import {DatetimeFormatter} from '@services/formatters/datetime/datetime-formatte
|
|||
})
|
||||
export class DateFormatter extends DatetimeFormatter {
|
||||
|
||||
toInternalFormat(dateString: string): string {
|
||||
if (!dateString) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const dateTime = DateTime.fromFormat(dateString, this.getInternalDateFormat());
|
||||
|
||||
if (dateTime.isValid) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return formatDate(dateString, this.getDateFormat(), this.locale);
|
||||
getInternalFormat(): string {
|
||||
return this.getInternalDateFormat();
|
||||
}
|
||||
|
||||
validateUserFormat(dateString: string): boolean {
|
||||
const dateTime = DateTime.fromFormat(dateString, this.getInternalDateFormat());
|
||||
return !dateTime.isValid;
|
||||
}
|
||||
|
||||
toUserFormat(dateString: string): string {
|
||||
return formatDate(dateString, this.getDateFormat(), this.locale);
|
||||
getUserFormat(): string {
|
||||
return this.getDateFormat();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -82,8 +82,16 @@ export class DatetimeFormatter implements Formatter {
|
|||
return dateFormat + ' ' + timeFormat;
|
||||
}
|
||||
|
||||
getInternalFormat(): string {
|
||||
return this.getInternalDateTimeFormat();
|
||||
}
|
||||
|
||||
getUserFormat(): string {
|
||||
return this.getDateTimeFormat();
|
||||
}
|
||||
|
||||
toUserFormat(dateString: string): string {
|
||||
return formatDate(dateString, this.getDateTimeFormat(), this.locale);
|
||||
return formatDate(dateString, this.getUserFormat(), this.locale);
|
||||
}
|
||||
|
||||
toInternalFormat(dateString: string): string {
|
||||
|
@ -91,32 +99,69 @@ export class DatetimeFormatter implements Formatter {
|
|||
return '';
|
||||
}
|
||||
|
||||
const dateTime = DateTime.fromFormat(dateString, this.getInternalDateTimeFormat());
|
||||
const dateTime = this.toDateTime(dateString);
|
||||
|
||||
if (dateTime.isValid) {
|
||||
if (!dateTime.isValid) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return formatDate(dateString, this.getInternalDateTimeFormat(), this.locale);
|
||||
return formatDate(dateTime.toJSDate(), this.getInternalFormat(), this.locale);
|
||||
}
|
||||
|
||||
fromUserDate(datetime: string): NgbDateStruct {
|
||||
toDateTime(datetimeString: string): DateTime {
|
||||
if (!datetimeString) {
|
||||
return DateTime.invalid('empty');
|
||||
}
|
||||
|
||||
let dateTime = this.fromUserFormat(datetimeString);
|
||||
|
||||
if (!dateTime.isValid) {
|
||||
dateTime = this.fromInternalFormat(datetimeString);
|
||||
}
|
||||
|
||||
return dateTime;
|
||||
}
|
||||
|
||||
userFormatToStruct(datetime: string): NgbDateStruct {
|
||||
if (!datetime) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const dateTime = DateTime.fromFormat(datetime, this.getDateFormat());
|
||||
const dateTime = this.toDateTime(datetime);
|
||||
|
||||
if (!dateTime.isValid) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const date = dateTime.toISODate().split('-');
|
||||
return {
|
||||
day: parseInt(date[2], 10),
|
||||
month: parseInt(date[1], 10),
|
||||
year: parseInt(date[0], 10)
|
||||
day: dateTime.day,
|
||||
month: dateTime.month,
|
||||
year: dateTime.year
|
||||
};
|
||||
}
|
||||
|
||||
fromUserFormat(datetime: string): DateTime {
|
||||
|
||||
datetime = datetime.replace('a', 'A');
|
||||
datetime = datetime.replace('p', 'P');
|
||||
datetime = datetime.replace('m', 'M');
|
||||
|
||||
let format = this.getUserFormat();
|
||||
|
||||
format = format.replace('aaaaa\'m\'', 'a');
|
||||
|
||||
return DateTime.fromFormat(datetime, format);
|
||||
}
|
||||
|
||||
fromInternalFormat(datetime: string): DateTime {
|
||||
|
||||
const format = this.getInternalFormat();
|
||||
|
||||
return DateTime.fromFormat(datetime, format);
|
||||
}
|
||||
|
||||
validateUserFormat(dateString: string): boolean {
|
||||
const dateTime = DateTime.fromFormat(dateString, this.getInternalDateTimeFormat());
|
||||
const dateTime = this.fromUserFormat(dateString);
|
||||
return !dateTime.isValid;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
import {ViewMode} from '@app-common/views/view.model';
|
||||
|
||||
export interface FormatOptions {
|
||||
[key: string]: any;
|
||||
|
||||
mode?: ViewMode;
|
||||
}
|
||||
|
||||
export interface Formatter {
|
||||
|
|
|
@ -56,7 +56,7 @@ export class NumberFormatter implements Formatter {
|
|||
|
||||
let pattern = '^(';
|
||||
pattern += '(\\d{1,3}(\\' + group + '\\d{3})*)|';
|
||||
pattern += '\\d*)';
|
||||
pattern += '\\d*';
|
||||
pattern += ')$';
|
||||
return pattern;
|
||||
}
|
||||
|
|
|
@ -26,6 +26,6 @@ export class PhoneFormatter implements Formatter {
|
|||
}
|
||||
|
||||
getUserFormatPattern(): string {
|
||||
return '^([+]?|00)(([(]{0,1}[0-9]{1,4}[)]{0,1})\\s*)*|([-\\s\\./0-9])*$';
|
||||
return '^([\\+]?|00)((([(]{0,1}\\s*[0-9]{1,4}\\s*[)]{0,1})\\s*)*|([\\-\\s\\./0-9])*)+$';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,13 +5,14 @@ import {FormControl} from '@angular/forms';
|
|||
import {Field, FieldDefinition} from '@app-common/record/field.model';
|
||||
import {Injectable} from '@angular/core';
|
||||
import {ValidationManager} from '@services/record/validation/validation.manager';
|
||||
import {DataTypeFormatter} from '@services/formatters/data-type.formatter.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class FieldManager {
|
||||
|
||||
constructor(protected validationManager: ValidationManager) {
|
||||
constructor(protected validationManager: ValidationManager, protected typeFormatter: DataTypeFormatter) {
|
||||
}
|
||||
|
||||
public buildShallowField(type: string, value: string): Field {
|
||||
|
@ -50,6 +51,8 @@ export class FieldManager {
|
|||
const validators = this.validationManager.getValidations(record.module, viewField, record);
|
||||
const asyncValidators = this.validationManager.getAsyncValidations(record.module, viewField, record);
|
||||
|
||||
const formattedValue = this.typeFormatter.toUserFormat(viewField.type, value, {mode: 'edit'});
|
||||
|
||||
const field = {
|
||||
type: viewField.type,
|
||||
value,
|
||||
|
@ -58,7 +61,7 @@ export class FieldManager {
|
|||
},
|
||||
definition,
|
||||
labelKey: viewField.label,
|
||||
formControl: new FormControl(value, validators, asyncValidators),
|
||||
formControl: new FormControl(formattedValue, validators, asyncValidators),
|
||||
validators,
|
||||
asyncValidators
|
||||
} as Field;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue