Show Validation Message on submit in Angular 4 Reactive Forms

前端 未结 3 1994
不思量自难忘°
不思量自难忘° 2021-02-09 01:39

I am using Angular 4, Reactive forms.I want to show validation error message when the user clicks on Submit/Create Account button. Here is the HTML and typescript code that I a

相关标签:
3条回答
  • 2021-02-09 02:07

    To get the properties like touched, dirty, valid of form control through formGroup use :

    signupForm.controls.firstname.touched. same way you can get the other properties like valid and invalid.

    if you have created individual object of FormControl then, you can access that object properties as firstname.touched without using formGroup.

    for more information about the validation in Model Driven Forms refer Model Driven Form Validations here.

    0 讨论(0)
  • 2021-02-09 02:09

    Something like

    onSubmit() {
        console.log(this.signupForm)
        this.signupForm.controls['firstname'].markAsTouched()
    }
    
    0 讨论(0)
  • 2021-02-09 02:19

    you can do this using from markAllAsTouched() method. it will mark all the form controls as touched and by doing so will trigger validation errors where needed

    onSubmit() {
        this.signupForm.markAllAsTouched();
    }
    
    0 讨论(0)
提交回复
热议问题