0

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,

<form [formGroup]="form" (ngSubmit)="onSubmit(form.value)" class="nobottommargin adminloginform" novalidate>
<label class="col-sm-4 text-right norightpadding">First Name</label>
     <div class="input-group" [ngClass]="{errmsg: (!form.controls['password'].valid ||
   (!form.controls['password'].pristine && submitted ))}">
    <input type="text" [formControl]="form.controls['lastname']" >
  </div>
<div class="col-sm-8">
          <button type="submit" >Register now</button>
        </div>

My ts,

    export class SignUp {
    submitted: boolean = false;
constructor(public fbld: FormBuilder, http: Http, public config: Config, public router: Router) {
    this.http = http;
    this.form = fbld.group({
        firstname: ['', Validators.required],
    });
    this.header = this.config.header1;
}
    onSubmit(form: ISignup): any {
        this.submitted = true;
     }
    }
2
  • What does "its not working mean exactly"? Commented Oct 20, 2016 at 6:04
  • Validations are not applied when i click submit button with an empty form Commented Oct 20, 2016 at 6:06

2 Answers 2

1

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');
      }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is working because you clicked the submit button. We are asking how show validation errors in typescript mode, executing a method, not a click form...
0

AskConge(){

this.congeService.askConge().subscribe((response) =>{  
  this.router.navigate(['/dashboard/conge/mes-demandes']).then(() => {
    window.location.reload()
    console.log('Post Done')
  })
}, (error) => {
  console.log(error);
})

}

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.