How to show validation on button click in typescript

前端 未结 1 1870
轮回少年
轮回少年 2021-01-26 16:57

I want to show up validation for the fields which are empty and for that i used the following method but its not working.Can anyone suggest me help.Thanks.

My template,<

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-26 17:22

    Please check below solution.

    DEMO : https://plnkr.co/edit/mJFftirG3ATDpnJRWmKN?p=preview

    export class AppComponent {
      form: FormGroup;
    
      constructor(private formBuilder: FormBuilder){
          this.submitted=false;
           this.form = formBuilder.group({
            lastname: ['', Validators.required],
          });
       }
    
       onSubmit(form)
       {
          this.submitted=true;
    
          console.log(form);
          if(form.valid)    //<<<### submit form is it is valid only
          {
            console.log('form submitted');
          }
        }
    }
    

    0 讨论(0)
提交回复
热议问题