From 9e843e36fa53c4290459c110a31be884b8867e24 Mon Sep 17 00:00:00 2001 From: Vincenzo Mecca Date: Fri, 4 Nov 2022 10:43:16 +0100 Subject: [PATCH] [1950] [DURACOM-101] Regex validator improved feat: - New regexp to validate pattern used for regex validation; --- .../form/builder/parsers/field-parser.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/app/shared/form/builder/parsers/field-parser.ts b/src/app/shared/form/builder/parsers/field-parser.ts index 86a7d99e41..e2a907056e 100644 --- a/src/app/shared/form/builder/parsers/field-parser.ts +++ b/src/app/shared/form/builder/parsers/field-parser.ts @@ -1,7 +1,7 @@ -import {Inject, InjectionToken} from '@angular/core'; +import { Inject, InjectionToken } from '@angular/core'; -import uniqueId from 'lodash/uniqueId'; -import {DynamicFormControlLayout, DynamicFormControlRelation, MATCH_VISIBLE, OR_OPERATOR} from '@ng-dynamic-forms/core'; +import { uniqueId } from 'lodash'; +import { DynamicFormControlLayout, DynamicFormControlRelation, MATCH_VISIBLE, OR_OPERATOR } from '@ng-dynamic-forms/core'; import { hasValue, isNotEmpty, isNotNull, isNotUndefined } from '../../../empty.util'; import { FormFieldModel } from '../models/form-field.model'; @@ -22,6 +22,7 @@ export const SUBMISSION_ID: InjectionToken = new InjectionToken( export const CONFIG_DATA: InjectionToken = new InjectionToken('configData'); export const INIT_FORM_VALUES: InjectionToken = new InjectionToken('initFormValues'); export const PARSER_OPTIONS: InjectionToken = new InjectionToken('parserOptions'); +export const REGEX_FIELD_VALIDATOR: RegExp = new RegExp('(\\/?)(.+)\\1([gimsuy]*)', 'i'); export abstract class FieldParser { @@ -43,7 +44,7 @@ export abstract class FieldParser { public abstract modelFactory(fieldValue?: FormFieldMetadataValueObject, label?: boolean): any; public parse() { - if (((this.getInitValueCount() > 1 && !this.configData.repeatable) || (this.configData.repeatable)) + if (((this.getInitValueCount() > 1 && !this.configData.repeatable) || (this.configData.repeatable)) && (this.configData.input.type !== ParserType.List) && (this.configData.input.type !== ParserType.Tag) ) { @@ -315,6 +316,7 @@ export abstract class FieldParser { * fields in type bind, made up of a 'match' outcome (make this field visible), an 'operator' * (OR) and a 'when' condition (the bindValues array). * @param configuredTypeBindValues array of types from the submission definition (CONFIG_DATA) + * @param typeField * @private * @return DynamicFormControlRelation[] array with one relation in it, for type bind matching to show a field */ @@ -344,7 +346,13 @@ export abstract class FieldParser { } protected addPatternValidator(controlModel) { - const regex = new RegExp(this.configData.input.regex); + const validatorMatcher = this.configData.input.regex.match(REGEX_FIELD_VALIDATOR); + let regex; + if (validatorMatcher != null && validatorMatcher.length > 3) { + regex = new RegExp(validatorMatcher[2], validatorMatcher[3]); + } else { + regex = new RegExp(this.configData.input.regex); + } controlModel.validators = Object.assign({}, controlModel.validators, { pattern: regex }); controlModel.errorMessages = Object.assign( {},