diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/core/form-field-validator.ts b/ng2-components/ng2-activiti-form/src/components/widgets/core/form-field-validator.ts index 60b243b38a..625a259968 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/core/form-field-validator.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/core/form-field-validator.ts @@ -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; + } +} diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/core/form-field.model.ts b/ng2-components/ng2-activiti-form/src/components/widgets/core/form-field.model.ts index b4821e37ed..3e3ed036dc 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/core/form-field.model.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/core/form-field.model.ts @@ -19,14 +19,21 @@ import { FormWidgetModel } from './form-widget.model'; import { FormFieldOption } from './form-field-option'; import { FormFieldTypes } from './form-field-types'; import { FormFieldMetadata } from './form-field-metadata'; -import { FormFieldValidator, RequiredFieldValidator, NumberFieldValidator } from './form-field-validator'; import { FormModel } from './form.model'; import { WidgetVisibilityModel } from '../../../models/widget-visibility.model'; +import { + FormFieldValidator, + RequiredFieldValidator, + NumberFieldValidator, + MinLengthFieldValidator +} from './form-field-validator'; + export class FormFieldModel extends FormWidgetModel { private _value: string; private _readOnly: boolean = false; + private _isValid: boolean = true; fieldType: string; id: string; @@ -36,6 +43,11 @@ export class FormFieldModel extends FormWidgetModel { overrideId: boolean; tab: string; colspan: number = 1; + minLength: number = 0; + maxLength: number = 0; + minValue: string; + maxValue: string; + regexPattern: string; options: FormFieldOption[] = []; restUrl: string; restResponsePath: string; @@ -58,6 +70,7 @@ export class FormFieldModel extends FormWidgetModel { set value(v: any) { this._value = v; + this.validate(); this.updateForm(); } @@ -68,17 +81,23 @@ export class FormFieldModel extends FormWidgetModel { 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) { for (let i = 0; i < this.validators.length; i++) { 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) { @@ -98,6 +117,11 @@ export class FormFieldModel extends FormWidgetModel { this.restIdProperty = json.restIdProperty; this.restLabelProperty = json.restLabelProperty; this.colspan = json.colspan; + this.minLength = json.minLength || 0; + this.maxLength = json.maxLength || 0; + this.minValue = json.minValue; + this.maxValue = json.maxValue; + this.regexPattern = json.regexPattern; this.options = json.options || []; this.hasEmptyValue = json.hasEmptyValue; this.className = json.className; @@ -113,7 +137,8 @@ export class FormFieldModel extends FormWidgetModel { this.validators = [ new RequiredFieldValidator(), - new NumberFieldValidator() + new NumberFieldValidator(), + new MinLengthFieldValidator() ]; } diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/core/form.model.ts b/ng2-components/ng2-activiti-form/src/components/widgets/core/form.model.ts index 40b16001f4..b3079ec4d6 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/core/form.model.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/core/form.model.ts @@ -153,7 +153,7 @@ export class FormModel { this._isValid = true; let fields = this.getFormFields(); for (let i = 0; i < fields.length; i++) { - if (!fields[i].isValid()) { + if (!fields[i].validate()) { this._isValid = false; return; } @@ -164,7 +164,7 @@ export class FormModel { if (!field) { return; } - if (!field.isValid()) { + if (!field.validate()) { this._isValid = false; return; }