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-collapse": "Collapse",
"group-expand": "Expand", "group-expand": "Expand",
"group-collapse-help": "Click here to collapse", "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> [ngClass]="getClass('element', 'hint')"></small>
<div *ngIf="showErrorMessages" [ngClass]="[getClass('element', 'errors'), getClass('grid', 'errors')]"> <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> </div>
</ng-container> </ng-container>

View File

@@ -156,7 +156,7 @@ export class FormBuilderService extends DynamicFormService {
Object.keys(groupValue) Object.keys(groupValue)
.forEach((key) => { .forEach((key) => {
const normValue = normalizeValue(controlModel, groupValue[key], groupIndex); const normValue = normalizeValue(controlModel, groupValue[key], groupIndex);
if (normValue.hasValue()) { if (isNotEmpty(normValue) && normValue.hasValue()) {
if (iterateResult.hasOwnProperty(key)) { if (iterateResult.hasOwnProperty(key)) {
iterateResult[key].push(normValue); iterateResult[key].push(normValue);
} else { } else {

View File

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

View File

@@ -194,6 +194,11 @@ export abstract class FieldParser {
this.markAsRequired(controlModel); this.markAsRequired(controlModel);
} }
if (this.hasRegex()) {
console.log(this.configData.input.regex);
this.addPatternValidator(controlModel);
}
// Available Languages // Available Languages
if (this.configData.languageCodes && this.configData.languageCodes.length > 0) { if (this.configData.languageCodes && this.configData.languageCodes.length > 0) {
(controlModel as DsDynamicInputModel).languageCodes = this.configData.languageCodes; (controlModel as DsDynamicInputModel).languageCodes = this.configData.languageCodes;
@@ -202,6 +207,20 @@ export abstract class FieldParser {
return controlModel; 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) { protected markAsRequired(controlModel) {
controlModel.required = true; controlModel.required = true;
controlModel.validators = Object.assign({}, controlModel.validators, {required: null}); controlModel.validators = Object.assign({}, controlModel.validators, {required: null});

View File

@@ -71,7 +71,10 @@ describe('RowParser test suite', () => {
row2 = { row2 = {
fields: [ fields: [
{ {
input: {type: 'onebox'}, input: {
type: 'onebox',
regex: '^[a-zA-Z0-9]+$'
},
label: 'Title', label: 'Title',
mandatory: 'false', mandatory: 'false',
repeatable: true, repeatable: true,

View File

@@ -122,14 +122,8 @@ export class NumberPickerComponent implements OnInit, ControlValueAccessor {
} }
writeValue(value) { 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) { registerOnChange(fn) {
return return