Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private handleFreeForm(field: Ro.IField, fieldEntry: string) {
if (field.isScalar()) {
const mandatoryError = validateMandatory(field, fieldEntry);
if (mandatoryError) {
return this.returnResult('', this.validationMessage(mandatoryError, new Ro.Value(''), field.extensions().friendlyName()));
}
let value = new Ro.Value(fieldEntry);
if (Ro.isDateOrDateTime(field)) {
const dt = validateDate(fieldEntry, supportedDateFormats);
if (dt) {
value = new Ro.Value(Ro.toDateString(dt.toDate()));
}
}
// if optional but empty always valid
if (fieldEntry != null && fieldEntry !== '') {
const remoteMask = field.extensions().mask();
const localFilter = this.mask.toLocalFilter(remoteMask, field.extensions().format()!);
const validateError = validateMandatoryAgainstType(field, fieldEntry, localFilter);
if (validateError) {
return this.returnResult('', this.validationMessage(validateError, value, field.extensions().friendlyName()));
}
}
export function validateDateFormat(model: Models.IHasExtensions, newValue: Date | string, filter: ILocalFilter): string {
const range = model.extensions().range();
const newDate = (newValue instanceof Date) ? newValue : getDate(newValue);
if (range && newDate) {
const min = range.min ? getDate(range.min as string) : null;
const max = range.max ? getDate(range.max as string) : null;
if (min && newDate < min) {
return Msg.outOfRange(Models.toDateString(newDate), Models.getUtcDate(range.min as string), Models.getUtcDate(range.max as string), filter);
}
if (max && newDate > max) {
return Msg.outOfRange(Models.toDateString(newDate), Models.getUtcDate(range.min as string), Models.getUtcDate(range.max as string), filter);
}
}
return '';
}
export function validateDateFormat(model: Models.IHasExtensions, newValue: Date | string, filter: ILocalFilter): string {
const range = model.extensions().range();
const newDate = (newValue instanceof Date) ? newValue : getDate(newValue);
if (range && newDate) {
const min = range.min ? getDate(range.min as string) : null;
const max = range.max ? getDate(range.max as string) : null;
if (min && newDate < min) {
return Msg.outOfRange(Models.toDateString(newDate), Models.getUtcDate(range.min as string), Models.getUtcDate(range.max as string), filter);
}
if (max && newDate > max) {
return Msg.outOfRange(Models.toDateString(newDate), Models.getUtcDate(range.min as string), Models.getUtcDate(range.max as string), filter);
}
}
return '';
}
export function setScalarValueInView(vm: { value: string | number | boolean | Date | null }, propertyRep: Ro.PropertyMember, value: Ro.Value) {
if (Ro.isDate(propertyRep)) {
const date = Ro.toUtcDate(value);
vm.value = date ? Ro.toDateString(date) : '';
} else if (Ro.isDateTime(propertyRep)) {
const date = Ro.toUtcDate(value);
vm.value = date ? Ro.toDateTimeString(date) : '';
} else if (Ro.isTime(propertyRep)) {
const time = Ro.toTime(value);
vm.value = time ? Ro.toTimeString(time) : '';
} else {
vm.value = value.scalar();
}
}