How to trigger Angular 2 input validation manually?

后端 未结 3 1335
萌比男神i
萌比男神i 2021-02-01 13:06

I have two inputs: - First one on which I apply my custom validator - Second one which value I use in my custom validator (it is dynamic and editable)

If I apply my cust

相关标签:
3条回答
  • 2021-02-01 13:41

    You can update the validity of a formControl

    form.controls['myControl'].updateValueAndValidity();

    0 讨论(0)
  • 2021-02-01 13:43

    I found this to be much better

    this.myForm.markAllAsTouched();
    
    0 讨论(0)
  • 2021-02-01 14:06

    If you have template-driven form you can access form throw ViewChild decorator:

    @ViewChild('myForm') public form: NgForm;
    

    then, validate one field or the whole form group using method mentioned above:

    this.form.controls.myControl.updateValueAndValidity();
    
    0 讨论(0)
提交回复
热议问题