M
M
msdosx862018-08-09 10:19:01
JavaScript
msdosx86, 2018-08-09 10:19:01

Why is angular ignoring the validator?

this.myform = new FormGroup({
      'name': new FormControl('', [
        Validators.required,
        Validators.minLength(3),
        Validators.maxLength(20)
      ]),
      'email': new FormControl('', [
        Validators.required,
        Validators.email
      ]),
      'password': new FormControl('', [
        Validators.required,
        Validators.pattern(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/)
      ]),
      'password_repeat': new FormControl('', [
        Validators.required
      ])
    }, this.passwordMatchValidator)

the second argument to FormGroup is an additional validator that doesn't work at all. Tried { validator: this.passwordMatchValidator }, still ignores. Does this mean that if all controls have their own validators, then the additional one will be ignored?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Mazhekin, 2018-08-10
@mazhekin

this.myform = new FormGroup({ though it should, use this.formBuilder.group

constructor(private formBuilder: FormBuilder)  {}
    
ngOnInit() {
    this.myform = this.formBuilder.group({
        name: new FormControl('', [.....
           ....
        }, {
            validator:  myComponent.myGroupValidator
        })
}

static myGroupValidator(control: AbstractControl): { [key: string]: boolean } | null { 
    // тут в дебаге проверьте, что заходит сюда
    ....
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question