Guest User

sign up ts

a guest
Nov 6th, 2017
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. import { Component, OnInit } from '@angular/core';
  2. import { AuthService, SocialUser } from 'angular4-social-login';
  3. import { FacebookLoginProvider, GoogleLoginProvider } from 'angular4-social-login';
  4. import { PasswordValidation } from './password-validation';
  5. import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
  6.  
  7. @Component({
  8. selector: 'app-root',
  9. templateUrl: './app.component.html',
  10. styleUrls: ['./app.component.css']
  11. })
  12. export class AppComponent implements OnInit {
  13. //private user: SocialUser;
  14. private loggedIn: boolean;
  15. name: string = '';
  16. title = 'app';
  17. rForm: FormGroup;
  18. user = {email: '', password: ''};
  19. constructor(private authService: AuthService, private formBuilder: FormBuilder) {}
  20.  
  21. ngOnInit() {
  22. // this.authService.authState.subscribe((user) => {
  23. // this.user = user;
  24. // console.log(user);
  25. // this.loggedIn = (user != null);
  26. // });
  27. this.validate();
  28. }
  29.  
  30. validate() {
  31. this.rForm = this.formBuilder.group({
  32. 'email': [null, Validators.compose([Validators.required, Validators.pattern('^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,6}$')])],
  33. 'password': [null, [Validators.compose([Validators.required, Validators.minLength(8), this.NoWhitespaceValidator])]],
  34. 'confpassword': [null, [this.matchOtherValidator('password')]],
  35. 'name': [null, Validators.compose([Validators.required, Validators.minLength(2), this.NoWhitespaceValidator])],
  36. 'dob': [null, Validators.required],
  37. 'gender': [null, Validators.required]
  38. });
  39. }
  40.  
  41. public NoWhitespaceValidator(control: FormControl) {
  42. let isWhitespace = (control.value || '').trim().length === 0 ;
  43. let isValid = !isWhitespace;
  44. return isValid ? null : { 'whitespace': true };
  45. }
  46.  
  47. onSignIn() {
  48. console.log('hello');
  49. this.authService.signIn(GoogleLoginProvider.PROVIDER_ID);
  50. }
  51. signOut(): void {
  52. this.authService.signOut();
  53. }
  54.  
  55. matchOtherValidator (otherControlName: string) {
  56. let thisControl: FormControl;
  57. let otherControl: FormControl;
  58.  
  59. return function matchOtherValidate (control: FormControl) {
  60. if (!control.parent) {
  61. return null;
  62. }
  63. // Initializing the validator.
  64. if (!thisControl) {
  65. thisControl = control;
  66. otherControl = control.parent.get(otherControlName) as FormControl;
  67. if (!otherControl) {
  68. throw new Error('matchOtherValidator(): other control is not found in parent group');
  69. }
  70. otherControl.valueChanges.subscribe(() => {
  71. thisControl.updateValueAndValidity();
  72. });
  73. }
  74.  
  75. if (!otherControl) {
  76. return null;
  77. }
  78.  
  79. if (otherControl.value !== thisControl.value) {
  80. return {
  81. matchOther: true
  82. };
  83. }
  84.  
  85. return null;
  86.  
  87. };
  88.  
  89. }
  90. doSignup(): void {
  91. if (!this.rForm.valid) {
  92. this.validateAllFormFields(this.rForm);
  93. } else {
  94. console.log('submitted');
  95. }
  96. }
  97.  
  98. validateAllFormFields(formGroup: FormGroup) {
  99. Object.keys(formGroup.controls).forEach(field => {
  100. const control = formGroup.get(field);
  101. if (control instanceof FormControl) {
  102. control.markAsTouched({ onlySelf: true });
  103. } else if (control instanceof FormGroup) {
  104. this.validateAllFormFields(control);
  105. }
  106. });
  107. }
  108. }
Add Comment
Please, Sign In to add comment