get composite field

This commit is contained in:
annampak 2017-11-20 10:46:09 +02:00
parent 2c45254e53
commit 8b59910404
2 changed files with 63 additions and 35 deletions

View File

@ -70,14 +70,14 @@ export class DynamicFormFieldComponent {
if (field.visible == true) if (field.visible == true)
return true; return true;
} }
FieldValueRuleMethod(field, rule) { //fieldValue rule -- checks the value of target and apply rules, at the same time when the field becomes visible FieldValueRuleMethod(field, rule, targetField) { //fieldValue rule -- checks the value of target and apply rules, at the same time when the field becomes visible /calling the AddvalidationRules we apply the validation rules for the new field
var targetField = this.dataModel.getFieldByKey(rule._target); //calling the AddvalidationRules we apply the validation rules for the new field
var fieldValue = this.form.get(field.key).value;//to do: change field.value var fieldValue = this.form.get(field.key).value;//to do: change field.value
if (rule._ruleStyle == "range") { if (rule._ruleStyle == "range") {
if (parseInt(rule._from) < parseInt(field.value) && parseInt(field.value) < parseInt(rule._to)) { if (parseInt(rule._from) < parseInt(field.value) && parseInt(field.value) < parseInt(rule._to)) {
console.log("visible" + field.value) console.log("visible" + field.value)
targetField.visible = true; targetField.visible = true;
this.AddvalidationRules(rule._target); this.AddvalidationRules(targetField);
} else { } else {
this.hideField(targetField, rule); this.hideField(targetField, rule);
} }
@ -87,7 +87,7 @@ export class DynamicFormFieldComponent {
let ruleValue = rule.value.__text; let ruleValue = rule.value.__text;
if (field.value.toString() == ruleValue) { //field.value.value.toString() == ruleValue if (field.value.toString() == ruleValue) { //field.value.value.toString() == ruleValue
targetField.visible = true; targetField.visible = true;
this.AddvalidationRules(rule._target); this.AddvalidationRules(targetField);
} else { } else {
this.hideField(targetField, rule); this.hideField(targetField, rule);
} }
@ -95,7 +95,7 @@ export class DynamicFormFieldComponent {
if (rule._ruleStyle == "checked") { //checkbox field if (rule._ruleStyle == "checked") { //checkbox field
if (field.value == true) { if (field.value == true) {
targetField.visible = true; targetField.visible = true;
this.AddvalidationRules(rule._target); this.AddvalidationRules(targetField);
} else { } else {
this.hideField(targetField, rule); this.hideField(targetField, rule);
} }
@ -104,7 +104,7 @@ export class DynamicFormFieldComponent {
if (field.value !== "") { if (field.value !== "") {
if (field.value == false) { if (field.value == false) {
targetField.visible = true; targetField.visible = true;
this.AddvalidationRules(rule._target); this.AddvalidationRules(targetField);
} else { } else {
this.hideField(targetField, rule); this.hideField(targetField, rule);
} }
@ -114,7 +114,7 @@ export class DynamicFormFieldComponent {
if (rule._ruleStyle == "existence") { if (rule._ruleStyle == "existence") {
if (field.visible == "true" || field.visible == true) { if (field.visible == "true" || field.visible == true) {
targetField.visible = true; targetField.visible = true;
this.AddvalidationRules(rule._target); this.AddvalidationRules(targetField);
} else { } else {
this.hideField(targetField, rule); this.hideField(targetField, rule);
} }
@ -122,7 +122,7 @@ export class DynamicFormFieldComponent {
if (rule._ruleStyle == "regex") { if (rule._ruleStyle == "regex") {
if (new RegExp(rule.__cdata).test(field.value)) { if (new RegExp(rule.__cdata).test(field.value)) {
targetField.visible = true; targetField.visible = true;
this.AddvalidationRules(rule._target); this.AddvalidationRules(targetField);
} else { } else {
this.hideField(targetField, rule); this.hideField(targetField, rule);
} }
@ -141,7 +141,7 @@ export class DynamicFormFieldComponent {
ruleValue = rule.value.__text; ruleValue = rule.value.__text;
if (fieldValue.toString() == ruleValue || ruleValueBoolean.valueOf() == true) { //field.value.value.toString() == ruleValue if (fieldValue.toString() == ruleValue || ruleValueBoolean.valueOf() == true) { //field.value.value.toString() == ruleValue
targetField.visible = true; targetField.visible = true;
this.AddvalidationRules(rule._target); this.AddvalidationRules(targetField);
} else { } else {
this.hideField(targetField, rule); this.hideField(targetField, rule);
} }
@ -151,10 +151,31 @@ export class DynamicFormFieldComponent {
hideField(targetField, rule) { hideField(targetField, rule) {
targetField.visible = false; targetField.visible = false;
targetField.value = ' '; targetField.value = ' ';
if (this.form.controls[rule._target].hasError("pattern")) if (this.form.controls[targetField.key].hasError("pattern"))
this.form.controls[rule._target].reset(); //the regex error message didn't remove without field reset this.form.controls[targetField.key].reset(); //the regex error message didn't remove without field reset
this.form.controls[rule._target].clearValidators(); // when a field is hidden must clear the validators and the errors this.form.controls[targetField.key].clearValidators(); // when a field is hidden must clear the validators and the errors
this.form.controls[rule._target].updateValueAndValidity(); this.form.controls[targetField.key].updateValueAndValidity();
}
findTargetField (field, rule){
// var targetField = this.dataModel.getFieldByKey(rule._target);
let targetFields = new Array();
if (this.dataModel.getFieldByKey(rule._target) == undefined) {
this.dataModel.groups.forEach(gr => {
if (gr.key == rule._target) {
gr.groupFields.forEach(field=>{
targetFields.push(field);
});
if(gr.compositeFields)
gr.compositeFields.groupFields.forEach(field=>{
targetFields.push(field);
});
}
})
} else {
targetFields.push(this.dataModel.getFieldByKey(rule._target));
}
return targetFields;
} }
toggleVisibility(e, field, ckb) { //ckb the checkbox only send this parameter, it's essential to change the field value toggleVisibility(e, field, ckb) { //ckb the checkbox only send this parameter, it's essential to change the field value
@ -165,24 +186,31 @@ export class DynamicFormFieldComponent {
if (field.rules.length != undefined && field.rules.length > 1) if (field.rules.length != undefined && field.rules.length > 1)
field.rules.forEach(rule => { field.rules.forEach(rule => {
if (rule._type == "fieldValue") { if (rule._type == "fieldValue") {
this.FieldValueRuleMethod(field, rule); let targetFieldsArray = this.findTargetField(field,rule);
targetFieldsArray.forEach(targetField =>{
this.FieldValueRuleMethod(field, rule, targetField);
})
} }
}); });
else if (field.rules._type == "fieldValue") { else if (field.rules._type == "fieldValue") {
this.FieldValueRuleMethod(field, field.rules); let targetFieldsArray = this.findTargetField(field,field.rules);
targetFieldsArray.forEach(targetField =>{
this.FieldValueRuleMethod(field, field.rules, targetField);
})
} }
} }
AddvalidationRules(field) { AddvalidationRules(field) {
if (this.dataModel.getFieldByKey(field).attributes.validation != undefined) { if (this.dataModel.getFieldByKey(field.key).attributes.validation != undefined) {
let arrayVal = new Array(); let arrayVal = new Array();
this.dataModel.getFieldByKey(field).attributes.validation.forEach(rule => { this.dataModel.getFieldByKey(field.key).attributes.validation.forEach(rule => {
if (rule.ruleStyle.toString() == RuleStyle[RuleStyle.existence]) { if (rule.ruleStyle.toString() == RuleStyle[RuleStyle.existence]) {
this.dataModel.getFieldByKey(field).required = true; this.dataModel.getFieldByKey(field.key).required = true;
arrayVal.push(Validators.required); arrayVal.push(Validators.required);
} }
if (rule.ruleStyle.toString() == RuleStyle[RuleStyle.regex]) { if (rule.ruleStyle.toString() == RuleStyle[RuleStyle.regex]) {
this.dataModel.getFieldByKey(field).regex = rule.regex; this.dataModel.getFieldByKey(field.key).regex = rule.regex;
arrayVal.push(Validators.pattern(rule.regex)); arrayVal.push(Validators.pattern(rule.regex));
} }
if (rule.ruleStyle.toString() == RuleStyle[RuleStyle.customValidation]) { if (rule.ruleStyle.toString() == RuleStyle[RuleStyle.customValidation]) {
@ -190,8 +218,8 @@ export class DynamicFormFieldComponent {
} }
}); });
this.form.controls[field].setValidators(arrayVal); //Multiple Validators, Usage of array because setValidator override any validators that are provided during initialistaion this.form.controls[field.key].setValidators(arrayVal); //Multiple Validators, Usage of array because setValidator override any validators that are provided during initialistaion
this.form.controls[field].updateValueAndValidity(); //hide--> visible must update the validators this.form.controls[field.key].updateValueAndValidity(); //hide--> visible must update the validators
} }

View File

@ -173,7 +173,7 @@ export class dataModelBuilder {
if (dataValues[fieldGroup._id][field.key] != undefined) // to put values in fields if (dataValues[fieldGroup._id][field.key] != undefined) // to put values in fields
field.value = dataValues[fieldGroup._id][field.key].value == undefined ? dataValues[fieldGroup._id][field.key] : dataValues[fieldGroup._id][field.key].value; field.value = dataValues[fieldGroup._id][field.key].value == undefined ? dataValues[fieldGroup._id][field.key] : dataValues[fieldGroup._id][field.key].value;
if(field.order.toString().split(",").length > 1){//--------------Composite Fields Multiplicity -------------------------------------------------- if(field.order.toString().split(".").length > 1){//--------------Composite Fields Multiplicity --------------------------------------------------
compositeFields.groupFields.push(field); compositeFields.groupFields.push(field);
newfldGroup.compositeFields = compositeFields; newfldGroup.compositeFields = compositeFields;
}else }else
@ -246,7 +246,7 @@ export class dataModelBuilder {
newAttribute.multiplicityMin = attr.multiplicity._min; newAttribute.multiplicityMin = attr.multiplicity._min;
newAttribute.ordinal = attr._ordinal newAttribute.ordinal = attr._ordinal
if (multiplicity){ if (multiplicity){
fields.find(x => x.key == newAttribute.id).order = fields.find(x => x.key == newAttribute.id).order +','+ newAttribute.ordinal; fields.find(x => x.key == newAttribute.id).order = fields.find(x => x.key == newAttribute.id).order +'.'+ newAttribute.ordinal;
fields.find(x => x.key == newAttribute.id).multiplicity = true; fields.find(x => x.key == newAttribute.id).multiplicity = true;
} }
newAttribute.ordinal = attr._ordinal; newAttribute.ordinal = attr._ordinal;
@ -305,7 +305,7 @@ export class dataModelBuilder {
fields.sort((a, b) => a.order - b.order);
return newAttribute; return newAttribute;
} }