Advertisement
hhac

data.component.ts

Oct 11th, 2018
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { FormGroup, FormControl, FormArray, Validators } from '@angular/forms';
  2. import { Component, OnInit } from '@angular/core';
  3. import { Observable } from 'rxjs/internal/Observable';
  4. // import { Observable } from 'rxjs/Rx';
  5.  
  6. /**
  7.  * data.component.ts
  8.  */
  9.  
  10. @Component({
  11.   selector: 'app-data',
  12.   templateUrl: './data.component.html',
  13.   styles: []
  14. })
  15. export class DataComponent {
  16.  
  17.   forma: FormGroup;
  18.  
  19.  
  20.  
  21.   usuario: Object = {
  22.     nombrecompleto: {
  23.       nombre: 'Hector',
  24.       apellido: 'Alpizar'
  25.     },
  26.     correo: 'hector.alpizar@mail.com',
  27.     // pasatiempos: ['Correr', 'Dormir', 'Comer']
  28.   };
  29.  
  30.   constructor() {
  31.     this.forma = new FormGroup({
  32.                                 'nombrecompleto': new FormGroup({
  33.                                   'nombre': new FormControl('', [
  34.                                     Validators.required,
  35.                                     Validators.minLength(5)
  36.                                   ]),
  37.                                   'apellido': new FormControl('', [
  38.                                     Validators.required,
  39.                                     this.noAlpizar
  40.                                   ])
  41.                                 }),
  42.                 'correo': new FormControl('', [
  43.                                                 Validators.required,
  44.                                                 Validators.pattern('[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$')
  45.                                               ]
  46.                                           ),
  47.                 'pasatiempos': new FormArray(
  48.                   [new FormControl('Correr', Validators.required)]
  49.                   // this.getPasatiempos()
  50.                 ),
  51.                 'usuario': new FormControl('', Validators.required, this.usuarioExiste),
  52.                 'password1': new FormControl('', Validators.required),
  53.                 'password2': new FormControl('')
  54.     });
  55.  
  56.     // this.forma.setValue(this.usuario);
  57.  
  58.     this.forma.controls['password2'].setValidators([
  59.       Validators.required,
  60.       this.noIgual.bind(this.forma)
  61.     ]);
  62.  
  63.     console.log(this.forma);
  64.   }
  65.  
  66.   guardarCambios() {
  67.     console.log('guardarCambios()');
  68.  
  69.     this.forma.reset({
  70.       nombrecompleto: {
  71.         nombre: '',
  72.         apellido: ''
  73.       },
  74.       correo: ''
  75.     });
  76.   }
  77.  
  78.   // getPasatiempos(): FormControl[] {
  79.   //   const fc: FormControl[] = [];
  80.   //   for (let pasatiempo of this.usuario.pasatiempos) {
  81.  
  82.   //     const temp = new FormControl(pasatiempo, Validators.required);
  83.  
  84.   //     console.log(temp.value);
  85.  
  86.   //     fc.push(temp);
  87.   //   }
  88.   //   return fc;
  89.   // }
  90.  
  91.   agregarPasatiempos() {
  92.  
  93.     console.log('agregarPasatiempos()');
  94.  
  95.     (<FormArray>this.forma.controls['pasatiempos']).
  96.       push(new FormControl('', Validators.required));
  97.  
  98.       console.log(this.forma);
  99.   }
  100.  
  101.   noAlpizar(control: FormControl): {[s: string]: boolean} {
  102.     if (control.value === 'Alpizar') {
  103.       return {
  104.        noalpizar: true
  105.       };
  106.     }
  107.  
  108.     return null;
  109.   }
  110.  
  111.   noIgual(control: FormControl): { [s: string]: boolean } {
  112.     const forma: any = this;
  113.  
  114.     if (control.value !== forma.controls['password1'].value) {
  115.  
  116.       return {
  117.         noiguales: true
  118.       };
  119.     }
  120.  
  121.     return null;
  122.   }
  123.  
  124.   usuarioExiste(control: FormControl): Promise<any> | Observable<any> {
  125.     const promise = new Promise((resolve, reject) => {
  126.       setTimeout(() => {
  127.         if (control.value === 'cholo') {
  128.           resolve( { usuarioexiste: true } );
  129.         } else {
  130.           resolve( null );
  131.         }
  132.       }, 3000);
  133.     });
  134.  
  135.     return promise;
  136.   }
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement