1- import { Inject , InjectionToken } from '@angular/core' ;
1+ import { Inject , InjectionToken } from '@angular/core' ;
22
33import uniqueId from 'lodash/uniqueId' ;
4- import { DynamicFormControlLayout , DynamicFormControlRelation , MATCH_VISIBLE , OR_OPERATOR } from '@ng-dynamic-forms/core' ;
4+ import {
5+ DynamicFormControlLayout ,
6+ DynamicFormControlRelation ,
7+ MATCH_VISIBLE ,
8+ OR_OPERATOR
9+ } from '@ng-dynamic-forms/core' ;
510
611import { hasValue , isNotEmpty , isNotNull , isNotUndefined } from '../../../empty.util' ;
712import { FormFieldModel } from '../models/form-field.model' ;
@@ -22,6 +27,12 @@ export const SUBMISSION_ID: InjectionToken<string> = new InjectionToken<string>(
2227export const CONFIG_DATA : InjectionToken < FormFieldModel > = new InjectionToken < FormFieldModel > ( 'configData' ) ;
2328export const INIT_FORM_VALUES : InjectionToken < any > = new InjectionToken < any > ( 'initFormValues' ) ;
2429export const PARSER_OPTIONS : InjectionToken < ParserOptions > = new InjectionToken < ParserOptions > ( 'parserOptions' ) ;
30+ /**
31+ * This pattern checks that a regex field uses the common ECMAScript format: `/{pattern}/{flags}`, in which the flags
32+ * are part of the regex, or a simpler one with only pattern `/{pattern}/` or `{pattern}`.
33+ * The regex itself is encapsulated inside a `RegExp` object, that will validate the pattern syntax.
34+ */
35+ export const REGEX_FIELD_VALIDATOR = new RegExp ( '(\\/?)(.+)\\1([gimsuy]*)' , 'i' ) ;
2536
2637export abstract class FieldParser {
2738
@@ -43,7 +54,7 @@ export abstract class FieldParser {
4354 public abstract modelFactory ( fieldValue ?: FormFieldMetadataValueObject , label ?: boolean ) : any ;
4455
4556 public parse ( ) {
46- if ( ( ( this . getInitValueCount ( ) > 1 && ! this . configData . repeatable ) || ( this . configData . repeatable ) )
57+ if ( ( ( this . getInitValueCount ( ) > 1 && ! this . configData . repeatable ) || ( this . configData . repeatable ) )
4758 && ( this . configData . input . type !== ParserType . List )
4859 && ( this . configData . input . type !== ParserType . Tag )
4960 ) {
@@ -315,6 +326,7 @@ export abstract class FieldParser {
315326 * fields in type bind, made up of a 'match' outcome (make this field visible), an 'operator'
316327 * (OR) and a 'when' condition (the bindValues array).
317328 * @param configuredTypeBindValues array of types from the submission definition (CONFIG_DATA)
329+ * @param typeField
318330 * @private
319331 * @return DynamicFormControlRelation[] array with one relation in it, for type bind matching to show a field
320332 */
@@ -343,8 +355,21 @@ export abstract class FieldParser {
343355 return hasValue ( this . configData . input . regex ) ;
344356 }
345357
358+ /**
359+ * Adds pattern validation to `controlModel`, it uses the encapsulated `configData` to test the regex,
360+ * contained in the input config, against the common `ECMAScript` standard validator {@link REGEX_FIELD_VALIDATOR},
361+ * and creates an equivalent `RegExp` object that will be used during form-validation against the user-input.
362+ * @param controlModel
363+ * @protected
364+ */
346365 protected addPatternValidator ( controlModel ) {
347- const regex = new RegExp ( this . configData . input . regex ) ;
366+ const validatorMatcher = this . configData . input . regex . match ( REGEX_FIELD_VALIDATOR ) ;
367+ let regex ;
368+ if ( validatorMatcher != null && validatorMatcher . length > 3 ) {
369+ regex = new RegExp ( validatorMatcher [ 2 ] , validatorMatcher [ 3 ] ) ;
370+ } else {
371+ regex = new RegExp ( this . configData . input . regex ) ;
372+ }
348373 controlModel . validators = Object . assign ( { } , controlModel . validators , { pattern : regex } ) ;
349374 controlModel . errorMessages = Object . assign (
350375 { } ,
0 commit comments