Progress bar improvements, validators

This commit is contained in:
annampak 2017-10-04 12:39:45 +03:00
parent 14335cce29
commit 6c0d9946ee
7 changed files with 121 additions and 75 deletions

View File

@ -1,5 +1,6 @@
export enum RuleStyle {
existence,
regex
regex,
customValidation
}

View File

@ -50,11 +50,10 @@
<div class="panel-footer">
{{dirtyValues}}
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="70"
aria-valuemin="0" aria-valuemax="100" [ngStyle]="{'width': dirtyValues + '%'}">
{{dirtyValues}}
{{dirtyValues}}%
</div>
</div>
<!-- <button type="button" class="btn btn-info" onclick="signOut();">Sign out</button> -->

View File

@ -49,18 +49,30 @@ export class DynamicFormComponent implements OnInit {
this.dataModel = this.dataModelService.getDataModel(data);
this.form = this.qcs.toFormGroup(this.dataModel.fields, this.dataModel.groups);
this.form.valueChanges.subscribe(data => {
console.log('Form changes', data);
// console.log('Form changes', data);
let dirtyValuesArray: Array<any> = [];
let count = 0;
let countDirtyValues = 0;
Object.keys(this.form.controls).forEach((c) => {
count++;
//count++;
let currentControl = this.form.controls[c];
if (currentControl.dirty)
dirtyValuesArray.push(currentControl.value);
});
var percentage = Math.floor(dirtyValuesArray.length * 100 / count);
this.dataModel.groups.forEach(grp => {
grp.groupFields.forEach((fld) => {
if (fld.visible == true || fld.visible == "true")
count++;
if(fld.value != undefined)
countDirtyValues++;
});
});
//console.log(count);
// var percentage = Math.floor(dirtyValuesArray.length * 100 / count);
var percentage = Math.floor(countDirtyValues * 100 / count);
this.dirtyValues = percentage;
})
@ -91,7 +103,8 @@ export class DynamicFormComponent implements OnInit {
this.onValueChanged(); // (re)set validation messages now
}
onValueChanged(data?: any) {debugger;
onValueChanged(data?: any) {
debugger;
if (!this.form) { return; }
const form = this.form;

View File

@ -1,10 +1,11 @@
import { DataModel } from '../../entities/DataModel';
import { Component, Input, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { FormGroup, ValidatorFn, AbstractControl } from '@angular/forms';
import { FieldBase } from './field-base';
import { GroupBase } from '../../form/dynamic-form-group/group-base';
import { DropdownField } from '../../form/fields/dropdown/field-dropdown';
import { RuleStyle } from '../../entities/common/rulestyle';
@Component({
@ -17,9 +18,18 @@ export class DynamicFormFieldComponent {
@Input() field: FieldBase<any>;
@Input() form: FormGroup;
get isValid() { debugger;
get isValid() {
return this.form.controls[this.field.key].valid;
}
get isValidRequired() {
return this.form.controls[this.field.key].hasError("required");
}
get isValidPattern() {
return this.form.controls[this.field.key].hasError("pattern");
}
get isValidCustom() {
return this.form.controls[this.field.key].hasError("forbiddenName");
}
public ngOnInit() { //dropdown lists take only one of the available sources
for (var i = 0, len = this.dataModel.groups.length; i < len; i++) {
@ -56,6 +66,7 @@ export class DynamicFormFieldComponent {
let ruleValue = rule.value.__text;
if (field.value.value.toString() == ruleValue) {
this.dataModel.getFieldByKey(rule._target).visible = true;
this.AddvalidationRules(rule._target);
} else {
this.dataModel.getFieldByKey(rule._target).visible = false;
}
@ -63,6 +74,7 @@ export class DynamicFormFieldComponent {
if (rule._ruleStyle == "checked") { //checkbox field
if (field.value == true) {
this.dataModel.getFieldByKey(rule._target).visible = true;
this.AddvalidationRules(rule._target);
} else {
this.dataModel.getFieldByKey(rule._target).visible = false;
}
@ -79,4 +91,25 @@ export class DynamicFormFieldComponent {
});
}
AddvalidationRules(field) {
if (this.dataModel.getFieldByKey(field).attributes.validation != undefined) {
this.dataModel.getFieldByKey(field).attributes.validation.forEach(rule => {
if (rule.ruleStyle.toString() == RuleStyle[RuleStyle.existence])
this.dataModel.getFieldByKey(field).required = true;
if (rule.ruleStyle.toString() == RuleStyle[RuleStyle.regex])
this.dataModel.getFieldByKey(field).regex = rule.regex;
if (rule.ruleStyle.toString() == RuleStyle[RuleStyle.customValidation])
this.form.controls[field].setValidators(this.forbiddenNameValidator(/bob/i));
});
}
}
forbiddenNameValidator(nameRe: RegExp): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } => {
const forbidden = nameRe.test(control.value);
return forbidden ? { 'forbiddenName': { value: control.value } } : null;
};
}
}

View File

@ -8,7 +8,7 @@ export class FieldBase<T>{
required:boolean;
order:number;
rules: Rule[];
visible: boolean;
visible: boolean | string;
controlType:string;
group:string;
description:string;
@ -22,7 +22,7 @@ export class FieldBase<T>{
required?:boolean,
order?: number,
rules?: Rule[],
visible?: boolean,
visible?: boolean | string,
controlType?: string
group?: string
description?: string,

View File

@ -75,7 +75,7 @@ export class dataModelBuilder {
newfield = new CheckBoxField({
label: element.title.__cdata,
key:element._id,
value: true,
value: element.value,
order:element._ordinal,
rules: element.visible.rule != undefined ? element.visible.rule: rule,
visible: element._defaultVisibility,
@ -91,7 +91,7 @@ export class dataModelBuilder {
newfield = new RadioBoxField({
label: element.title.__cdata,
key:element._id,
value: true,
value: element.value,
order:element._ordinal,
rules: element.visible.rule != undefined ? element.visible.rule: rule,
visible: element._defaultVisibility,

View File

@ -29,16 +29,16 @@ export class FieldControlService {
});
//PLEASE CHANGE THE group.key TO BE SAME AS THE ONE ON THE DYNAMIC-FORM-GROUP-COMPONENT.html
form[group.key] = new FormGroup(subgroup);
console.log("FORM_GROUP_GROUP: ");
console.log(form);
//console.log("FORM_GROUP_GROUP: ");
//console.log(form);
});
//add also the spare fields into the form
fields.forEach(field => {
form[field.key] = field.required ? new FormControl(field.value || '', Validators.required) : new FormControl(field.value || '')
});
console.log("FORM_GROUP: ");
console.log(form);
//console.log("FORM_GROUP: ");
//console.log(form);
return new FormGroup(form);
}