Added support for regex validation

This commit is contained in:
Giuseppe Digilio
2018-07-06 14:20:09 +02:00
parent 7315337c64
commit ccb50f69d6
7 changed files with 33 additions and 11 deletions

View File

@@ -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 }}."
}
}
}
}

View File

@@ -333,7 +333,7 @@
[ngClass]="getClass('element', 'hint')"></small>
<div *ngIf="showErrorMessages" [ngClass]="[getClass('element', 'errors'), getClass('grid', 'errors')]">
<small *ngFor="let message of errorMessages" class="invalid-feedback d-block">{{ message }}</small>
<small *ngFor="let message of errorMessages" class="invalid-feedback d-block">{{ message | translate:model.validators }}</small>
</div>
</ng-container>

View File

@@ -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 {

View File

@@ -26,6 +26,7 @@ export class FormFieldModel {
@autoserialize
input: {
type: string;
regex?: string;
};
@autoserialize

View File

@@ -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});

View File

@@ -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,

View File

@@ -122,14 +122,8 @@ 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;
}
}
registerOnChange(fn) {
return