mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-31 17:38:48 +00:00
#726 form field validators (base support)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
<label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect" [attr.for]="field.id">
|
||||
<input type="checkbox"
|
||||
[attr.id]="field.id"
|
||||
[attr.required]="isRequired()"
|
||||
class="mdl-checkbox__input"
|
||||
[(ngModel)]="field.value"
|
||||
(ngModelChange)="checkVisibility(field)"
|
||||
|
@@ -30,6 +30,7 @@ export class FormFieldTypes {
|
||||
static FUNCTIONAL_GROUP: string = 'functional-group';
|
||||
static PEOPLE: string = 'people';
|
||||
static BOOLEAN: string = 'boolean';
|
||||
static NUMBER: string = 'integer';
|
||||
|
||||
static READONLY_TYPES: string[] = [
|
||||
FormFieldTypes.HYPERLINK,
|
||||
|
@@ -0,0 +1,72 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { FormFieldModel } from './form-field.model';
|
||||
import { FormFieldTypes } from './form-field-types';
|
||||
|
||||
export interface FormFieldValidator {
|
||||
|
||||
isSupported(field: FormFieldModel): boolean;
|
||||
validate(field: FormFieldModel): boolean;
|
||||
|
||||
}
|
||||
|
||||
export class RequiredFieldValidator implements FormFieldValidator {
|
||||
|
||||
private supportedTypes = [
|
||||
FormFieldTypes.TEXT,
|
||||
FormFieldTypes.MULTILINE_TEXT,
|
||||
FormFieldTypes.NUMBER
|
||||
];
|
||||
|
||||
isSupported(field: FormFieldModel): boolean {
|
||||
return field &&
|
||||
field.required &&
|
||||
this.supportedTypes.indexOf(field.type) > -1;
|
||||
}
|
||||
|
||||
validate(field: FormFieldModel): boolean {
|
||||
if (this.isSupported(field)) {
|
||||
if (!field.value) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export class NumberFieldValidator implements FormFieldValidator {
|
||||
|
||||
private supportedTypes = [
|
||||
FormFieldTypes.NUMBER
|
||||
];
|
||||
|
||||
private pattern: string = '-?[0-9]*(\.[0-9]+)?';
|
||||
|
||||
isSupported(field: FormFieldModel): boolean {
|
||||
return field && this.supportedTypes.indexOf(field.type) > -1;
|
||||
}
|
||||
|
||||
validate(field: FormFieldModel): boolean {
|
||||
if (this.isSupported(field)) {
|
||||
return !(this.pattern && field.value && (field.value.length > 0) && !field.value.match(new RegExp('^' + this.pattern + '$')));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@@ -19,6 +19,7 @@ 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';
|
||||
|
||||
@@ -49,6 +50,8 @@ export class FormFieldModel extends FormWidgetModel {
|
||||
isVisible: boolean = true;
|
||||
visibilityCondition: WidgetVisibilityModel = null;
|
||||
|
||||
validators: FormFieldValidator[] = [];
|
||||
|
||||
get value(): any {
|
||||
return this._value;
|
||||
}
|
||||
@@ -67,9 +70,11 @@ export class FormFieldModel extends FormWidgetModel {
|
||||
|
||||
isValid(): boolean {
|
||||
|
||||
if (this.required) {
|
||||
if (this.type === FormFieldTypes.TEXT || this.type === FormFieldTypes.MULTILINE_TEXT) {
|
||||
return this._value ? true : false;
|
||||
if (this.validators && this.validators.length > 0) {
|
||||
for (let i = 0; i < this.validators.length; i++) {
|
||||
if (!this.validators[i].validate(this)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,6 +110,11 @@ export class FormFieldModel extends FormWidgetModel {
|
||||
this._value = this.parseValue(json);
|
||||
this.updateForm();
|
||||
}
|
||||
|
||||
this.validators = [
|
||||
new RequiredFieldValidator(),
|
||||
new NumberFieldValidator()
|
||||
];
|
||||
}
|
||||
|
||||
parseValue(json: any): any {
|
||||
|
@@ -124,6 +124,7 @@ export class FormModel {
|
||||
);
|
||||
}
|
||||
}
|
||||
this.validateForm();
|
||||
}
|
||||
|
||||
onFormFieldChanged(field: FormFieldModel) {
|
||||
|
@@ -27,3 +27,4 @@ export * from './container.model';
|
||||
export * from './tab.model';
|
||||
export * from './form-outcome.model';
|
||||
export * from './form-outcome-event.model';
|
||||
export * from './form-field-validator';
|
||||
|
@@ -3,6 +3,7 @@
|
||||
type="text"
|
||||
pattern="-?[0-9]*(\.[0-9]+)?"
|
||||
[attr.id]="field.id"
|
||||
[attr.required]="isRequired()"
|
||||
[(ngModel)]="field.value"
|
||||
(ngModelChange)="checkVisibility(field)"
|
||||
[disabled]="field.readOnly">
|
||||
|
Reference in New Issue
Block a user