]>
Commit | Line | Data |
---|---|---|
0f6da32b | 1 | import { Validators } from '@angular/common'; |
7da18e44 | 2 | import { Component, OnInit } from '@angular/core'; |
0f6da32b | 3 | import { FormGroup, FormControl, REACTIVE_FORM_DIRECTIVES } from '@angular/forms'; |
7da18e44 C |
4 | import { Router } from '@angular/router'; |
5 | ||
6 | import { UserService } from '../shared'; | |
7 | ||
8 | @Component({ | |
9 | selector: 'my-user-add', | |
10 | template: require('./user-add.component.html'), | |
0f6da32b | 11 | directives: [ REACTIVE_FORM_DIRECTIVES ] |
7da18e44 C |
12 | }) |
13 | export class UserAddComponent implements OnInit { | |
0f6da32b | 14 | userAddForm: FormGroup; |
7da18e44 | 15 | error: string = null; |
0f6da32b C |
16 | username = ''; |
17 | password = ''; | |
7da18e44 C |
18 | |
19 | constructor(private router: Router, private userService: UserService) {} | |
20 | ||
21 | ngOnInit() { | |
0f6da32b C |
22 | this.userAddForm = new FormGroup({ |
23 | username: new FormControl('', [ <any>Validators.required, <any>Validators.minLength(3), <any>Validators.maxLength(20) ]), | |
24 | password: new FormControl('', [ <any>Validators.required, <any>Validators.minLength(6) ]), | |
7da18e44 C |
25 | }); |
26 | } | |
27 | ||
0f6da32b | 28 | addUser() { |
7da18e44 C |
29 | this.error = null; |
30 | ||
0f6da32b | 31 | this.userService.addUser(this.username, this.password).subscribe( |
7da18e44 C |
32 | ok => this.router.navigate([ '/admin/users/list' ]), |
33 | ||
bf68dd75 | 34 | err => this.error = err.text |
7da18e44 C |
35 | ); |
36 | } | |
37 | } |