Guest User

Untitled

a guest
Nov 2nd, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. this.registerForm.controls['email'].setErrors({ invalid: true, valid: false })
  2.  
  3. export class RegistrationComponent implements OnInit {
  4.  
  5. ...
  6.  
  7. constructor(...) {}
  8.  
  9. ngOnInit() {
  10. this.registerForm = this.fb.group({
  11. email: ['', [FormValidationService.emailValidator]],
  12. password: this.fb.group({
  13. pwd: ['', [FormValidationService.passwordValidator]],
  14. confirm: ['', [FormValidationService.passwordValidator]],
  15. }, {validator: FormValidationService.confirmValidator('pwd', 'confirm')}),
  16. username: ['', [FormValidationService.shortStringValidator]],
  17. })
  18. }
  19.  
  20. register() {
  21. this.spinnerShow = !this.spinnerShow
  22. const regData = this.registerForm.value
  23. regData.confirm = this.registerForm.value.password.confirm
  24. regData.password = this.registerForm.value.password.pwd
  25.  
  26. this.auth.registration(regData).subscribe(
  27. (res: any) => {
  28. if (res.error) {
  29. this.spinnerShow = !this.spinnerShow
  30. const messages = res.messages
  31. for (const key in messages) {
  32. if (messages.hasOwnProperty(key)) {
  33. if (key === 'both') {
  34. this.bothErrMsg = messages[key]
  35. this.registerForm.controls['email'].setErrors({ notUnique: true })
  36. } else {
  37. this.registerForm.controls[key].setErrors({ errMsg: messages[key] })
  38. }
  39. }
  40. }
  41. } else {
  42. this.lc.setItem('user', res.user)
  43. this.matDialogRef.close()
  44. }
  45. },
  46. error => {
  47. console.log(error) // TODO
  48. this.spinnerShow = !this.spinnerShow
  49. }
  50. )
  51. }
  52. }
  53.  
  54. <div class="container">
  55. <h3>Register new user.</h3>
  56. <form [formGroup]="registerForm" novalidate>
  57. <mat-form-field>
  58. <input matInput formControlName="email" placeholder="Email" type="email" required>
  59. <mat-error *ngIf="registerForm.controls.email.hasError('errMsg') && registerForm.controls.email.touched">
  60. {{ registerForm.get('email').getError('errMsg') }}
  61. </mat-error>
  62. </mat-form-field>
  63.  
  64. <div class="error" *ngIf="bothErrMsg !== undefined">{{ bothErrMsg }}</div>
  65.  
  66. <button mat-raised-button color="primary" [disabled]="registerForm.invalid == true" (click)="register()">Sign Up</button>
  67. </form>
  68. </div>
  69.  
  70. <form [formGroup]="registerForm">
  71. <mat-form-field>
  72. <input matInput placeholder="Email" formControlName="email">
  73. <mat-error *ngIf="email.dirty && email.hasError('notUnique')">This email is already taken!</mat-error>
  74. </mat-form-field>
  75. </form>
  76.  
  77. public registerForm = new FormGroup({
  78. email: new FormControl(null, {
  79. validators: [Validators.required, Validators.email]
  80. })
  81. });
  82.  
  83. public get email(): AbstractControl {
  84. return this.registerForm.get('email')!;
  85. }
  86.  
  87. public register(): void {
  88. this.auth.registration(this.registerForm.value).subscribe((res) => {
  89. this.email.setErrors({ notUnique: true });
  90. });
  91. }
Add Comment
Please, Sign In to add comment