expand json-rules-engine to work with arrays and angular FormGroup
npm install json-rules-engine-angular-itanpm install json-rules-engine-angular-ITA
import { RuleProperties} from 'json-rules-engine-angular-ITA
const emailMatch = {
conditions: {
"all": [
{
"value": {
"fact":"main",
"path": "$.email"
},
"fact": "main",
"path": "$.emailConfirm",
"operator": "notEqual"
}
]
},
event: {
type: 'emailNotMatch',
params: {
formPath: 'emailConfirm'
}
},
name: 'emailCheck',
priority: 1
}
export const emailRules: RuleProperties[] = [emailMatch];
`
`
import { Component, NgZone, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms'
import { Engine } from 'json-rules-engine-angular-ITA;
import { initEngine ,rulesEngineRunZone} from 'json-rules-engine-angular-ITA;
import {emailRules} from '../rules/email.rules'
@Component({
selector: 'app-sample-email',
templateUrl: './sample-email.component.html',
styleUrls: ['./sample-email.component.scss']
})
export class SampleEmailComponent implements OnInit {
engine: Engine
emailForm: FormGroup = new FormGroup({
email: new FormControl('', [Validators.required]),
emailConfirm: new FormControl('', [Validators.required]),
});
constructor(private ngZone: NgZone) { }
ngOnInit(): void {
this.engine = initEngine(emailRules, this.emailForm);
this.emailForm.valueChanges.subscribe(res => {
this.ngZone.run(() => {
rulesEngineRunZone(this.engine,this.emailForm)
})
})
}
}
`
`
sample-email
error list: {{emailForm.controls.email.errors|json}}
חובה להכניס כתובת מייל
error list: {{emailForm.controls.emailConfirm.errors|json}}
כתובת מייל לא תואמת
{{emailForm.value|json}}
``