Guest User

Untitled

a guest
Nov 13th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. <div class="inscriptionContainer">
  2. <section class='inscritFormContent'>
  3. <form [formGroup]="loginForm" (ngSubmit)="onSubmit(loginForm.value)" >
  4. <mat-form-field>
  5. <input
  6. type="text"
  7. matInput
  8. placeholder="user"
  9. [formControl]="loginForm.controls.user" >
  10. <mat-error *ngIf="loginForm.controls.user.invalid">{{getErrorMessage('user')}}</mat-error>
  11. </mat-form-field>
  12.  
  13. <mat-form-field>
  14. <input
  15. type="text"
  16. matInput
  17. placeholder="email"
  18. [formControl]="loginForm.controls.email">
  19. <mat-error *ngIf="loginForm.controls.email.invalid">{{getErrorMessage('email')}}</mat-error>
  20. </mat-form-field>
  21.  
  22. <mat-form-field>
  23. <input
  24. type="password"
  25. matInput
  26. placeholder="password"
  27. [formControl]="loginForm.controls.password">
  28. <mat-error *ngIf="loginForm.controls.password.invalid">{{getErrorMessage('password')}}</mat-error>
  29. </mat-form-field>
  30.  
  31. <mat-form-field>
  32. <input
  33. type="password"
  34. matInput
  35. placeholder="confirm_password"
  36. [formControl]="loginForm.controls.confirm_password">
  37. <mat-error *ngIf="loginForm.controls.confirm_password.invalid">{{getErrorMessage('confirm_password')}}</mat-error>
  38. </mat-form-field>
  39.  
  40. <button mat-raised-button color="primary" [disabled]="!loginForm.valid">Submit</button>
  41. </form>
  42.  
  43. import { Component, OnInit } from '@angular/core';
  44. import { FormGroup, FormBuilder, Validators, FormControl } from '@angular/forms';
  45. import { PasswordValidator } from './password.validator';
  46.  
  47. @Component({
  48. selector: 'app-inscription',
  49. templateUrl: './inscription.component.html',
  50. styleUrls: ['./inscription.component.scss']
  51. })
  52.  
  53. export class InscriptionComponent implements OnInit {
  54. loginForm : FormGroup;
  55. constructor() {}
  56.  
  57. ngOnInit() {
  58. this.loginForm = new FormGroup({
  59. user: new FormControl(null, [Validators.required]),
  60. email: new FormControl(null, [Validators.required, Validators.email]),
  61. password: new FormControl('', Validators.compose([
  62. Validators.minLength(6),
  63. Validators.required,
  64. ])),
  65. confirm_password: new FormControl('', Validators.required)
  66. }, (formGroup: FormGroup) => {
  67. return PasswordValidator.areEqual(formGroup);
  68. });
  69. }
  70.  
  71.  
  72. onSubmit(objValue) {
  73. console.log(this.loginForm.valid);
  74. console.log('objValue',objValue);
  75. }
  76. getErrorMessage(formControlName : string): string {
  77. const errors : any= {
  78. required : "Required Information",
  79. minlength: "The password need at least 6 characters",
  80. email : "The mail is not valid",
  81. areEqual:"The passwords are not the same"
  82. }
  83. return Object.keys(this.loginForm.controls[formControlName].errors).reduce(
  84. (prev, current, currentIndex) => {
  85.  
  86. return `${errors[current]}`;
  87. },''
  88. )
  89.  
  90. }
  91.  
  92. }
  93.  
  94. import { FormControl, FormGroup, NgForm, FormGroupDirective } from '@angular/forms';
  95.  
  96. export class PasswordValidator {
  97. static areEqual(formGroup: FormGroup) {
  98. let password;
  99. let passwordconfirmation;
  100. let valid = true;
  101.  
  102. for (let key in formGroup.controls) {
  103. if(key === password && password !== undefined){
  104. password = formGroup.controls[key].value
  105. }
  106.  
  107. if(key === confirm_password && confirm_password !== undefined){
  108. passwordconfirm = formGroup.controls[key].value
  109. }
  110. }
  111.  
  112.  
  113. if (password !== confirmpassword) {
  114. valid = false;
  115. }
  116.  
  117. if (valid) {
  118. return null;
  119. }
  120.  
  121. return {
  122. areEqual: true
  123. };
  124. }
  125. }
Add Comment
Please, Sign In to add comment