Skip to content

Instantly share code, notes, and snippets.

@vmussak
Created October 4, 2018 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vmussak/80c2ff2db76ad4bb167f44c81916b162 to your computer and use it in GitHub Desktop.
Save vmussak/80c2ff2db76ad4bb167f44c81916b162 to your computer and use it in GitHub Desktop.
onSubmit(form) {
if (!this.saving) {
for (const control in form.controls) {
if (form.controls.hasOwnProperty(control)) {
form.controls[control].markAsTouched();
form.controls[control].markAsDirty();
}
}
if (!form.valid) {
this.element.nativeElement.querySelectorAll('form .ng-invalid')[0].focus();
return false;
}
this.saving = true;
this.clienteService.cadastrar(this.info).subscribe(data => {
this.saving = false;
UiSnackbar.show({
text: `Cliente cadastrado com sucesso.`
});
this.router.navigate(['/cliente'], { replaceUrl: true });
}, e => {
this.saving = false;
if(e.error.statusCode == 406){
form.controls.cpf.setErrors({ duplicate: true });
this.element.nativeElement.querySelector('#cpf').focus();
} else {
UiSnackbar.show({
text: 'Ocorreu um erro interno, tente novamente mais tarde.'
});
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment