I was trying to use extra parameter map provided by Angular FormBuilder
group(controlsConfig: {
[key: string]: any;
}, extra: {
[key: string]: any;
} | null = null): FormGroup
Docs : FormBuilder
But facing
this.validator is not a function
If i pass a single validator instead of an array, the above error vanishes but the validation does not happen?
Can someone help me with this or provide the correct way of using the extra map parameter?
My component and its corresponding template is as follows:
app.component.ts
import { Component } from '@angular/core';
import { FormControl, Validators, FormGroup, FormBuilder } from '@angular/forms';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
formGroupNew: FormGroup;
constructor(private fb: FormBuilder) {
this.createForm();
}
createForm() {
this.formGroupNew = this.fb.group(
{ name: "Provide name"},
{ validator: [Validators.required, Validators.maxLength(5)] } );
}
validate() {
console.log(this.formGroupNew.controls["name"].status);
}
}
app.component.html:
<div class="container" [formGroup]="formGroupNew">
<input class="form-control" formControlName="name">
<button (click)="validate()">Validate</button>
</div>
name: ['', [Validators.required, Validators.minLength(2)]]