mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-05-19 17:14:57 +00:00
#726 performance improvements, minLength validator
- don’t re-evaluate validation state within isValid getter in order not to kill performance when binding UI to isValid
This commit is contained in:
parent
9e31cf10be
commit
c5b5a93372
@ -70,3 +70,24 @@ export class NumberFieldValidator implements FormFieldValidator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class MinLengthFieldValidator implements FormFieldValidator {
|
||||||
|
|
||||||
|
private supportedTypes = [
|
||||||
|
FormFieldTypes.TEXT,
|
||||||
|
FormFieldTypes.MULTILINE_TEXT
|
||||||
|
];
|
||||||
|
|
||||||
|
isSupported(field: FormFieldModel): boolean {
|
||||||
|
return field &&
|
||||||
|
field.minLength > 0 &&
|
||||||
|
this.supportedTypes.indexOf(field.type) > -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
validate(field: FormFieldModel): boolean {
|
||||||
|
if (this.isSupported(field)) {
|
||||||
|
return field.value.length >= field.minLength;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -19,14 +19,21 @@ import { FormWidgetModel } from './form-widget.model';
|
|||||||
import { FormFieldOption } from './form-field-option';
|
import { FormFieldOption } from './form-field-option';
|
||||||
import { FormFieldTypes } from './form-field-types';
|
import { FormFieldTypes } from './form-field-types';
|
||||||
import { FormFieldMetadata } from './form-field-metadata';
|
import { FormFieldMetadata } from './form-field-metadata';
|
||||||
import { FormFieldValidator, RequiredFieldValidator, NumberFieldValidator } from './form-field-validator';
|
|
||||||
import { FormModel } from './form.model';
|
import { FormModel } from './form.model';
|
||||||
import { WidgetVisibilityModel } from '../../../models/widget-visibility.model';
|
import { WidgetVisibilityModel } from '../../../models/widget-visibility.model';
|
||||||
|
import {
|
||||||
|
FormFieldValidator,
|
||||||
|
RequiredFieldValidator,
|
||||||
|
NumberFieldValidator,
|
||||||
|
MinLengthFieldValidator
|
||||||
|
} from './form-field-validator';
|
||||||
|
|
||||||
|
|
||||||
export class FormFieldModel extends FormWidgetModel {
|
export class FormFieldModel extends FormWidgetModel {
|
||||||
|
|
||||||
private _value: string;
|
private _value: string;
|
||||||
private _readOnly: boolean = false;
|
private _readOnly: boolean = false;
|
||||||
|
private _isValid: boolean = true;
|
||||||
|
|
||||||
fieldType: string;
|
fieldType: string;
|
||||||
id: string;
|
id: string;
|
||||||
@ -36,6 +43,11 @@ export class FormFieldModel extends FormWidgetModel {
|
|||||||
overrideId: boolean;
|
overrideId: boolean;
|
||||||
tab: string;
|
tab: string;
|
||||||
colspan: number = 1;
|
colspan: number = 1;
|
||||||
|
minLength: number = 0;
|
||||||
|
maxLength: number = 0;
|
||||||
|
minValue: string;
|
||||||
|
maxValue: string;
|
||||||
|
regexPattern: string;
|
||||||
options: FormFieldOption[] = [];
|
options: FormFieldOption[] = [];
|
||||||
restUrl: string;
|
restUrl: string;
|
||||||
restResponsePath: string;
|
restResponsePath: string;
|
||||||
@ -58,6 +70,7 @@ export class FormFieldModel extends FormWidgetModel {
|
|||||||
|
|
||||||
set value(v: any) {
|
set value(v: any) {
|
||||||
this._value = v;
|
this._value = v;
|
||||||
|
this.validate();
|
||||||
this.updateForm();
|
this.updateForm();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,17 +81,23 @@ export class FormFieldModel extends FormWidgetModel {
|
|||||||
return this._readOnly;
|
return this._readOnly;
|
||||||
}
|
}
|
||||||
|
|
||||||
isValid(): boolean {
|
get isValid(): boolean {
|
||||||
|
return this._isValid;
|
||||||
|
}
|
||||||
|
|
||||||
|
validate(): boolean {
|
||||||
|
// TODO: consider doing that on value setter and caching result
|
||||||
if (this.validators && this.validators.length > 0) {
|
if (this.validators && this.validators.length > 0) {
|
||||||
for (let i = 0; i < this.validators.length; i++) {
|
for (let i = 0; i < this.validators.length; i++) {
|
||||||
if (!this.validators[i].validate(this)) {
|
if (!this.validators[i].validate(this)) {
|
||||||
return false;
|
this._isValid = false;
|
||||||
|
return this._isValid;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
this._isValid = true;
|
||||||
|
return this._isValid;
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(form: FormModel, json?: any) {
|
constructor(form: FormModel, json?: any) {
|
||||||
@ -98,6 +117,11 @@ export class FormFieldModel extends FormWidgetModel {
|
|||||||
this.restIdProperty = json.restIdProperty;
|
this.restIdProperty = json.restIdProperty;
|
||||||
this.restLabelProperty = json.restLabelProperty;
|
this.restLabelProperty = json.restLabelProperty;
|
||||||
this.colspan = <number> json.colspan;
|
this.colspan = <number> json.colspan;
|
||||||
|
this.minLength = <number> json.minLength || 0;
|
||||||
|
this.maxLength = <number> json.maxLength || 0;
|
||||||
|
this.minValue = json.minValue;
|
||||||
|
this.maxValue = json.maxValue;
|
||||||
|
this.regexPattern = json.regexPattern;
|
||||||
this.options = <FormFieldOption[]> json.options || [];
|
this.options = <FormFieldOption[]> json.options || [];
|
||||||
this.hasEmptyValue = <boolean> json.hasEmptyValue;
|
this.hasEmptyValue = <boolean> json.hasEmptyValue;
|
||||||
this.className = json.className;
|
this.className = json.className;
|
||||||
@ -113,7 +137,8 @@ export class FormFieldModel extends FormWidgetModel {
|
|||||||
|
|
||||||
this.validators = [
|
this.validators = [
|
||||||
new RequiredFieldValidator(),
|
new RequiredFieldValidator(),
|
||||||
new NumberFieldValidator()
|
new NumberFieldValidator(),
|
||||||
|
new MinLengthFieldValidator()
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,7 +153,7 @@ export class FormModel {
|
|||||||
this._isValid = true;
|
this._isValid = true;
|
||||||
let fields = this.getFormFields();
|
let fields = this.getFormFields();
|
||||||
for (let i = 0; i < fields.length; i++) {
|
for (let i = 0; i < fields.length; i++) {
|
||||||
if (!fields[i].isValid()) {
|
if (!fields[i].validate()) {
|
||||||
this._isValid = false;
|
this._isValid = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -164,7 +164,7 @@ export class FormModel {
|
|||||||
if (!field) {
|
if (!field) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!field.isValid()) {
|
if (!field.validate()) {
|
||||||
this._isValid = false;
|
this._isValid = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user