I don’t know how to compare the fields with passwords for the function of the validator, below is a function that validates the fields with passwords, but this validator can only hang on one field and how to do the same function for two

this.form = new FormGroup({ 'name': new FormControl(null, [Validators.required]), 'email': new FormControl(null, [Validators.required, Validators.email], this.forbiddenEmails.bind(this)), 'pass': new FormControl(null, [Validators.required, Validators.minLength(6)]), 'conf_pass': new FormControl(null, [Validators.required, Validators.minLength(6)], this.confirm_pass.bind(this)) }) confirm_pass(control: FormControl): Promise<any>{ const val = this.form.value; return new Promise((resolve, reject) => { if (control.value !== val.pass){ resolve({confirm_pass: true}); }else { resolve(null); } }) } 

    1 answer 1

    You need to add your custom validator:

     private passEqual() { return (group: FormGroup) => { return (!group.dirty || !group.touched) || group.get('pass').value === group.get('conf_pass').value ? null : { custom: 'пароли не совпадают' }; } } 

    And add it to the builder call:

     this.form = new FormGroup({ 'name': new FormControl(null, [Validators.required]), 'email': new FormControl(null, [Validators.required, Validators.email], this.forbiddenEmails.bind(this)), 'password': new FormGroup({ 'pass': new FormControl(null, [Validators.required, Validators.minLength(6)]), 'conf_pass': new FormControl(null, [Validators.required, Validators.minLength(6)], this.confirm_pass.bind(this)) }, this.passEqual()), }) 
    • for some reason it gives the error Cannot find control with name: 'pass' although such a field was - Teapot
    • formGroupName = "password" add parent for block of passwords - Kostiantyn Okhotnyk
    • <div formGroupName="password"> .... <input formControlName="pass"/> <input formControlName="conf_pass"/> .... </div> - Kostiantyn Okhotnyk
    • thank you, blunted =) - Teapot