I have a data driven form in angular2 like below
this.formBuilder.group({
\'name\': [\'\',Validators.required],
\'description\': [\'\', Validators.required],
Validators.required
does the magic for you:
// using required
this.formGroup = this.formBuilder.group({
taskTreeId: [Common.UID()],
executionProgrammedTime: ["", [Validators.required]],
description: [""],
tasks: this.formBuilder.array([], Validators.required)
});
// using custom validation
export const minLengthArray = (min: number) => {
return (c: AbstractControl): {[key: string]: any} => {
if (c.value.length >= min)
return null;
return { MinLengthArray: true};
}
}
this.formGroup = this.formBuilder.group({
taskTreeId: [Common.UID()],
executionProgrammedTime: ["", [Validators.required]],
description: [""],
tasks: this.formBuilder.array([], minLengthArray(2))
});