Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multiple stage form validation angular

I am working on the form with validation on multiple stages, few elements validate normally, few elements validate on blur and other elements validate on submit. Validations are not working correctly. If someone knows better way of handling it then please let me know. Thanks

Here is my code:

constructor(
  private changeDetectorRef: ChangeDetectorRef,
  private el: ElementRef,
  private _snackBar: MatSnackBar ) {
  this.form = new FormGroup({});
  this.form.addControl(`firstname`, new FormControl(''));
  this.form.addControl(`lastname`, new FormControl(''));
  this.form.addControl(`title`, new FormControl('', 
  Validators.compose([Validators.required])));
this.form.addControl(
  `email`,
  new FormControl(
    '',
    Validators.compose([
      Validators.required,
      Validators.pattern('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$'),
    ])
  )
);
this.form.addControl(
  `phoneNumber`,
  new FormControl('', Validators.compose([Validators.required, Validators.pattern('^[0-9]*$')]))
);
this.form.addControl(
  `termsAndConditions`,
  new FormControl('', Validators.compose([Validators.required]))
);

this.changeDetectorRef.markForCheck();
}


addValidation() {
this.form.controls['firstname'].setValidators([Validators.required, Validators.minLength(2)]);
this.form.controls['lastname'].setValidators([Validators.required, Validators.minLength(2)]);
 }
 removeValidation() {
   this.form.controls['firstname'].setValidators([]);
   this.form.controls['lastname'].setValidators([]);
}

 onSubmit() {
    this.addValidation();
    // rest of my logic
}

onCancelBtnClick() {
  this.form.reset();
  this.removeValidation();
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There is a cleaner way of doing it. Make some changes in the code in the constructor and then you will not need addValidation and removeValidation methods. Forexample here we are applying validations on firstname and lastname on submit and on email on blur.

  this.form.addControl(`firstname`, new FormControl('', {
  validators: Validators.compose([Validators.required, Validators.minLength(2)]), updateOn: 'submit'
}));
this.form.addControl(`lastname`, new FormControl('', {
  validators: Validators.compose([Validators.required, Validators.minLength(2)]), updateOn: 'submit'
}));
this.form.addControl(`title`, new FormControl(''));
this.form.addControl(
  `email`,
  new FormControl(
    '',
    Validators.compose([
    '', {
    validators: Validators.compose([
      Validators.required,
      Validators.pattern('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$'),
    ])
    ]), updateOn: 'blur'
  }
  )
);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading