I want to create two or more methods from custom validation for checking the recovery password, but I can not get more than two arguments in the form builder, the message always appears:
Construct a new {@link FormGroup} with the given map of configuration. Valid keys for the extra parameter map are validator and asyncValidator.
Expected 1-2 arguments, but got 3.
One method works, but the second method don't work
The methods:
static MatchPreviousPassword(AC: AbstractControl) {
const previousPassword = 'guide';
const confirmPreviousPassword = AC.get('previousPassword').value;
if (previousPassword !== confirmPreviousPassword) {
console.log('false');
AC.get('previousPassword').setErrors({ MatchPassword: true });
} else {
console.log('true');
return null;
}
}
static MatchPassword(AC: AbstractControl) {
const password = AC.get('newPassword').value;
const confirmPassword = AC.get('confirmPassword').value;
if (password !== confirmPassword) {
console.log('false');
AC.get('confirmPassword').setErrors({ MatchPassword: true });
} else {
console.log('true');
return null;
}
}
The Form Builder:
buildForm() {
const min_char = 4;
const max_char = 10;
this.passwordForm = this.fb.group({
'previousPassword': [this.objAuthentication.password,
Validators.compose([ Validators.required, Validators.minLength(min_char), Validators.maxLength(max_char),])],
'newPassword': [this.objAuthentication.password,
Validators.compose([Validators.minLength(min_char), Validators.maxLength(max_char), Validators.required])],
'confirmPassword': [this.objAuthentication.password, Validators.required]
},
{
validator: UserPasswordResetComponent.MatchPassword << Trouble here!!!
});
}
This is how I have done it in my project (using reactive approach):
import {Component, OnInit} from '@angular/core';
import {FormGroup, FormControl, Validators} from "@angular/forms";
@Component({
selector: 'register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit{
registerForm: FormGroup;
ngOnInit() {
this.registerForm = new FormGroup({
'fullName': new FormControl(null, [Validators.minLength(2), Validators.required]),
'position': new FormControl(null, [Validators.required]),
'email': new FormControl(null, [Validators.email, Validators.required]),
'password': new FormControl(null, [Validators.minLength(6), Validators.required]),
'passwordRepeat': new FormControl(null, [Validators.minLength(6), Validators.required]),
'image': new FormControl([], []),
},
//two custom validators here:
[passwordMatch, myImageValidation]);
}
}
// password validator
export function passwordMatch(control: FormGroup) : {[key: string]: boolean}
{
return control.value.password !== control.value.passwordRepeat ? {'passwordMatch': true} : null;
}
// image validator just for example
export function myImageValidation(control: FormGroup) : {[key: string]: boolean}
{
return control.value.image !== null ? {'myImageValidation': true} : null;
}
Hope it will work for you. Let me know if you will have any questions. Good luck with coding.