diff --git a/resources/i18n/en.json b/resources/i18n/en.json index dc316af0bf..e24ece36ee 100644 --- a/resources/i18n/en.json +++ b/resources/i18n/en.json @@ -168,6 +168,11 @@ "group-collapse": "Collapse", "group-expand": "Expand", "group-collapse-help": "Click here to collapse", - "group-expand-help": "Click here to expand and add more element" + "group-expand-help": "Click here to expand and add more element", + "error": { + "validation": { + "pattern": "This input is restricted by the current pattern: {{ pattern }}." + } + } } } diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control.component.html b/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control.component.html index a6b17de5ed..db5bc92574 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control.component.html +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control.component.html @@ -333,7 +333,7 @@ [ngClass]="getClass('element', 'hint')">
- {{ message }} + {{ message | translate:model.validators }}
diff --git a/src/app/shared/form/builder/form-builder.service.ts b/src/app/shared/form/builder/form-builder.service.ts index 0fb8f9ef11..b87b9e4381 100644 --- a/src/app/shared/form/builder/form-builder.service.ts +++ b/src/app/shared/form/builder/form-builder.service.ts @@ -156,7 +156,7 @@ export class FormBuilderService extends DynamicFormService { Object.keys(groupValue) .forEach((key) => { const normValue = normalizeValue(controlModel, groupValue[key], groupIndex); - if (normValue.hasValue()) { + if (isNotEmpty(normValue) && normValue.hasValue()) { if (iterateResult.hasOwnProperty(key)) { iterateResult[key].push(normValue); } else { diff --git a/src/app/shared/form/builder/models/form-field.model.ts b/src/app/shared/form/builder/models/form-field.model.ts index faf51f14d3..95b8798d5f 100644 --- a/src/app/shared/form/builder/models/form-field.model.ts +++ b/src/app/shared/form/builder/models/form-field.model.ts @@ -26,6 +26,7 @@ export class FormFieldModel { @autoserialize input: { type: string; + regex?: string; }; @autoserialize diff --git a/src/app/shared/form/builder/parsers/field-parser.ts b/src/app/shared/form/builder/parsers/field-parser.ts index 52a7252bcf..638fe5ea59 100644 --- a/src/app/shared/form/builder/parsers/field-parser.ts +++ b/src/app/shared/form/builder/parsers/field-parser.ts @@ -194,6 +194,11 @@ export abstract class FieldParser { this.markAsRequired(controlModel); } + if (this.hasRegex()) { + console.log(this.configData.input.regex); + this.addPatternValidator(controlModel); + } + // Available Languages if (this.configData.languageCodes && this.configData.languageCodes.length > 0) { (controlModel as DsDynamicInputModel).languageCodes = this.configData.languageCodes; @@ -202,6 +207,20 @@ export abstract class FieldParser { return controlModel; } + protected hasRegex() { + return hasValue(this.configData.input.regex); + } + + protected addPatternValidator(controlModel) { + const regex = new RegExp(this.configData.input.regex); + controlModel.validators = Object.assign({}, controlModel.validators, {pattern: regex}); + controlModel.errorMessages = Object.assign( + {}, + controlModel.errorMessages, + {pattern: 'form.error.validation.pattern', regex: 'form.error.validation.pattern'}); + + } + protected markAsRequired(controlModel) { controlModel.required = true; controlModel.validators = Object.assign({}, controlModel.validators, {required: null}); diff --git a/src/app/shared/form/builder/parsers/row-parser.spec.ts b/src/app/shared/form/builder/parsers/row-parser.spec.ts index b7bb083de7..54a6bc6a27 100644 --- a/src/app/shared/form/builder/parsers/row-parser.spec.ts +++ b/src/app/shared/form/builder/parsers/row-parser.spec.ts @@ -71,7 +71,10 @@ describe('RowParser test suite', () => { row2 = { fields: [ { - input: {type: 'onebox'}, + input: { + type: 'onebox', + regex: '^[a-zA-Z0-9]+$' + }, label: 'Title', mandatory: 'false', repeatable: true, diff --git a/src/app/shared/number-picker/number-picker.component.ts b/src/app/shared/number-picker/number-picker.component.ts index 56be174055..99cc138d8c 100644 --- a/src/app/shared/number-picker/number-picker.component.ts +++ b/src/app/shared/number-picker/number-picker.component.ts @@ -122,13 +122,7 @@ export class NumberPickerComponent implements OnInit, ControlValueAccessor { } writeValue(value) { - if (this.startValue) { - this.startValue = this.value; - this.value = value; - } else { - // First init - this.startValue = value || this.min; - } + this.startValue = value || this.min; } registerOnChange(fn) {