How can I add multiple validators to a FormGroup.
A FormControl can accept an array of validators, however a FormGroup cannot. Is there a workaround aside from creating a single custom validator?
I am using rc4.
How can I add multiple validators to a FormGroup.
A FormControl can accept an array of validators, however a FormGroup cannot. Is there a workaround aside from creating a single custom validator?
I am using rc4.
Multiple validators can be combined through Validators.compose()
.
From the api reference:
compose(validators: ValidatorFn[]) : ValidatorFn
Compose multiple validators into a single function that returns the union of the individual error maps.
{ validator: Validators.compose( [ this.validationFn1.bind(this), this.validationFn2.bind(this) ] ) }
–
Highflier Actually, FormGroup did accept array of validators. Just that the interface not updated. Cast it to any will do. E.g.
<any>[Validators.required, Validators.minlength(2)]
I had the same problem. I was using a FormGroup and a FormBuilder. this is how i set two validators for email input:
emailForm : FormGroup;
CreatEmailForm(){
this.emailForm = this.formBuilder.group({
emial:["[email protected]", { validators: [Validator.required , Validators.email ]}],
context:[null , Validator.required]})
}
© 2022 - 2024 — McMap. All rights reserved.
let myGroup = this.formBuilder.group({}, {validator: Validators.compose([this.myCustomValidator(variable1, variable2)])});
Just to give you an idea of what myCustomValidator does, it returns a validator functionreturn (group: FormGroup): {[s: string]: boolean} => {}
The error I get is:Argument of type '((group: FormGroup) => { [s: string]: boolean; })[]' is not assignable to parameter of type 'ValidatorFn[]'.
– Justifiablelet myGroup = this.formBuilder.group({}, {validator: this.myCustomValidator(variable1, variable2)});
– Justifiable