Skip to content

Commit 3b76304

Browse files
committed
Form validations.
1 parent 64e33dd commit 3b76304

File tree

13 files changed

+145
-37
lines changed

13 files changed

+145
-37
lines changed

src/app/app.module.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BrowserModule } from '@angular/platform-browser';
2-
import { NgModule } from '@angular/core';
2+
import { NgModule, LOCALE_ID } from '@angular/core';
33
import { Routes, RouterModule } from '@angular/router';
44
import { HttpClientModule } from '@angular/common/http';
55
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
@@ -49,6 +49,7 @@ export const appRoutes: Routes = [
4949
RouterModule.forRoot(appRoutes)
5050
],
5151
providers: [
52+
{ provide: LOCALE_ID, useValue: 'pt-BR' },
5253
AuthenticationService,
5354
LoadingService
5455
],

src/app/domain/Jwt.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ export class Jwt {
33
Object.assign(this, init);
44
}
55

6-
Token: String;
6+
Token: string;
77
Timeout: Date;
8-
Username: String;
9-
Name: String;
10-
Email: String;
8+
Username: string;
9+
Name: string;
10+
Email: string;
1111
}

src/app/domain/card.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export class Card {
33
Object.assign(this, init);
44
}
55

6-
Title: String;
7-
Description: String;
8-
Link: String;
6+
Title: string;
7+
Description: string;
8+
Link: string;
99
}

src/app/domain/country.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export class Country {
33
Object.assign(this, init);
44
}
55

6-
Id: Number;
7-
Name: String;
6+
Id: number;
7+
Name: string;
88
}
99

src/app/domain/pagination.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export class Pagination<T> {
44
}
55

66
Items: Array<T>;
7-
Offset: Number;
8-
Limit: Number;
9-
Total: Number;
7+
Offset: number;
8+
Limit: number;
9+
Total: number;
1010
}
+88-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,89 @@
1-
export const CpfValidation = validator();
1+
import { AbstractControl, ValidatorFn } from '@angular/forms';
22

3-
function validator(): RegExp {
4-
return RegExp('(^(((0[1-9]|1[0-9]|2[0-8])[\/](0[1-9]|1[012]))|((29|30|31)[\/](0[13578]|1[02]))|((29|30)[\/](0[4,6,9]|11)))[\/](19|[2-9][0-9])\d\d$)|(^29[\/]02[\/](19|[2-9][0-9])(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)$)');
5-
}
3+
export const CpfValidation = validation();
4+
5+
function validation(): ValidatorFn {
6+
return (control: AbstractControl): { [key: string]: any } => {
7+
const cpf = control.value;
8+
9+
if (!cpf) {
10+
return { CpfInvalid: { value: cpf } };
11+
}
12+
13+
const value = cpf.replace(/[^0-9]/g, '');
14+
15+
if (value.length !== 11) {
16+
return { CpfInvalid: { value: cpf } };
17+
}
18+
19+
if ((value === '00000000000') ||
20+
(value === '11111111111') ||
21+
(value === '22222222222') ||
22+
(value === '33333333333') ||
23+
(value === '44444444444') ||
24+
(value === '55555555555') ||
25+
(value === '66666666666') ||
26+
(value === '77777777777') ||
27+
(value === '88888888888') ||
28+
(value === '99999999999')) {
29+
30+
return { CpfInvalid: { value: cpf } };
31+
}
32+
33+
let j: number = 10;
34+
let somatorio: number = 0;
35+
let numero: number = 0;
36+
let resto: number = 0;
37+
let digito1: number = 0;
38+
let digito2: number = 0;
39+
40+
let caracter: string = '';
41+
let auxiliar: string = '';
42+
43+
auxiliar = value.substring(0, 9);
44+
45+
for (let i: number = 0; i < 9; i++) {
46+
caracter = auxiliar.charAt(i);
47+
48+
numero = Number(caracter);
49+
somatorio = somatorio + (numero * j);
50+
51+
j--;
52+
}
53+
54+
resto = somatorio % 11;
55+
digito1 = 11 - resto;
56+
57+
if (digito1 > 9) {
58+
digito1 = 0;
59+
}
60+
61+
j = 11;
62+
somatorio = 0;
63+
auxiliar = auxiliar + digito1;
64+
65+
for (let i: number = 0; i < 10; i++) {
66+
caracter = auxiliar.charAt(i);
67+
68+
numero = Number(caracter);
69+
somatorio = somatorio + (numero * j);
70+
71+
j--;
72+
}
73+
74+
resto = somatorio % 11;
75+
digito2 = 11 - resto;
76+
77+
if (digito2 > 9) {
78+
digito2 = 0;
79+
}
80+
81+
auxiliar = auxiliar + digito2;
82+
83+
if (cpf !== auxiliar) {
84+
return { CpfInvalid: { value: cpf } };
85+
}
86+
87+
return null;
88+
};
89+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
1-
export const DateValidation = validator();
1+
import { AbstractControl, ValidatorFn } from '@angular/forms';
22

3-
function validator(): RegExp {
4-
return RegExp('(^(((0[1-9]|1[0-9]|2[0-8])[\/](0[1-9]|1[012]))|((29|30|31)[\/](0[13578]|1[02]))|((29|30)[\/](0[4,6,9]|11)))[\/](19|[2-9][0-9])\d\d$)|(^29[\/]02[\/](19|[2-9][0-9])(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)$)');
5-
}
3+
export const DateValidation = validation();
4+
5+
function validation(): ValidatorFn {
6+
return (control: AbstractControl): { [key: string]: any } => {
7+
8+
const date = control.value;
9+
10+
if (!date) {
11+
return { DateInvalid: { value: date } };
12+
}
13+
14+
let regex = RegExp('(^(((0[1-9]|1[0-9]|2[0-8])[\/](0[1-9]|1[012]))|((29|30|31)[\/](0[13578]|1[02]))|((29|30)[\/](0[4,6,9]|11)))[\/](19|[2-9][0-9])\d\d$)|(^29[\/]02[\/](19|[2-9][0-9])(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)$)');
15+
16+
if (regex.test(date)) {
17+
return { DateInvalid: { value: date } };
18+
}
19+
20+
return null;
21+
};
22+
}

src/app/routes/users/users-add/users-add.component.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { OverlayAnimation } from 'src/app/modules/animations/overlay.animation';
1010
import { Country } from 'src/app/domain/country';
1111
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
1212
import { DateValidation } from 'src/app/modules/validations/date.validation';
13+
import { CpfValidation } from 'src/app/modules/validations/cpf.validation';
1314

1415
@Component({
1516
selector: 'users-add',
@@ -38,19 +39,19 @@ export class UsersAddComponent {
3839
Name: this.formBuilder.control({
3940
value: '',
4041
disabled: false
41-
}, [ Validators.required ]),
42+
}, [ Validators.required, Validators.maxLength(30) ]),
4243
Email: this.formBuilder.control({
4344
value: '',
4445
disabled: false,
4546
}, [ Validators.required, Validators.email ]),
4647
Document: this.formBuilder.control({
4748
value: '',
4849
disabled: false
49-
}, [ Validators.required, Validators.minLength(6) ]),
50+
}, [ Validators.required, CpfValidation ]),
5051
Birthdate: this.formBuilder.control({
5152
value: '',
5253
disabled: false
53-
}, [ Validators.required, Validators.pattern(DateValidation) ]),
54+
}, [ Validators.required, DateValidation ]),
5455
Country: this.formBuilder.control({
5556
value: '',
5657
disabled: false

src/app/routes/users/users-edit/users-edit.component.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, ViewEncapsulation, Renderer2, ViewChild, ElementRef, Input } from '@angular/core';
1+
import { Component, ViewEncapsulation, Renderer2, ViewChild, ElementRef, LOCALE_ID, Inject } from '@angular/core';
22
import { faTimes, IconDefinition } from '@fortawesome/free-solid-svg-icons';
33
import { PanelAnimation } from 'src/app/modules/animations/panel.animation';
44
import { OverlayAnimation } from 'src/app/modules/animations/overlay.animation';
@@ -18,7 +18,7 @@ import { DateValidation } from 'src/app/modules/validations/date.validation';
1818
})
1919
export class UsersEditComponent {
2020
@ViewChild('panel') panel: ElementRef;
21-
21+
2222
public visible = new BehaviorSubject<Boolean>(false);
2323
public form: FormGroup;
2424
public user: User = new User;
@@ -29,7 +29,8 @@ export class UsersEditComponent {
2929
private formBuilder: FormBuilder,
3030
private renderer: Renderer2,
3131
private location: Location,
32-
private router: Router) {
32+
private router: Router,
33+
@Inject(LOCALE_ID) private locale: string) {
3334

3435
this.visible.subscribe((visible: Boolean) => {
3536
if (visible) {
@@ -45,11 +46,11 @@ export class UsersEditComponent {
4546
Document: this.formBuilder.control({
4647
value: this.user.Document,
4748
disabled: true
48-
}, [ Validators.required, Validators.minLength(6) ]),
49+
}),
4950
Birthdate: this.formBuilder.control({
50-
value: this.user.Birthdate.toLocaleDateString(),
51+
value: this.user.Birthdate.toLocaleDateString(locale),
5152
disabled: false
52-
}, [ Validators.required, Validators.pattern(DateValidation) ]),
53+
}, [ Validators.required, DateValidation ]),
5354
Country: this.formBuilder.control({
5455
value: this.user.Country,
5556
disabled: false
@@ -63,12 +64,12 @@ export class UsersEditComponent {
6364
disabled: false
6465
}),
6566
});
66-
67+
6768
this.renderer.addClass(document.body, 'overflow');
6869
this.renderer.setProperty(this.panel.nativeElement, 'scrollTop', '0');
6970
this.location.go(this.router.url.split('/')[1] + '/' + this.user.Id);
7071
} else {
71-
this.renderer.removeClass(document.body, 'overflow');
72+
this.renderer.removeClass(document.body, 'overflow');
7273
this.location.go(this.router.url.split('/')[1]);
7374
}
7475
});

src/app/routes/users/users-list/users-list.component.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<div class="col-2 col-md-1">
2525
<div class="form-group">
2626
<label class="small">Birthday</label>
27-
<label>{{ user.Birthdate.toLocaleDateString() }}</label>
27+
<label>{{ user.Birthdate.toLocaleDateString(locale) }}</label>
2828
</div>
2929
</div>
3030
<div class="col-2 col-md-1">

src/app/routes/users/users-list/users-list.component.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, ViewEncapsulation, Output, EventEmitter } from '@angular/core';
1+
import { Component, ViewEncapsulation, Output, EventEmitter, Inject, LOCALE_ID } from '@angular/core';
22
import { faTrash, faEdit } from '@fortawesome/free-solid-svg-icons';
33
import { ListAnimation } from './../../../modules/animations/list.animation';
44
import { User } from '../../../domain/user';
@@ -23,7 +23,9 @@ export class UsersListComponent {
2323
public faEdit: IconDefinition = faEdit;
2424
public pagination: Observable<Pagination<User>>;
2525

26-
constructor(public userService: HttpService<User>) { }
26+
constructor(
27+
public userService: HttpService<User>,
28+
@Inject(LOCALE_ID) public locale: string) { }
2729

2830
edit(user: User): void {
2931
this.editEvent.emit(user);

src/app/routes/users/users-page/users-page.component.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ export class UsersPageComponent implements OnInit {
4444
this.countries = results[1];
4545
});
4646
} else if (this.router.url.includes('users/')) {
47-
let get = this.userService.get('users', 1);
47+
let id = Number(this.router.url.split('users/')[1]);
48+
49+
let get = this.userService.get('users', id);
4850

4951
forkJoin([list, countries, get]).subscribe(results => {
5052
this.userEdit.user = results[2];

tslint.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"no-empty-interface": true,
6161
"no-eval": true,
6262
"no-inferrable-types": [
63-
true,
63+
false,
6464
"ignore-params"
6565
],
6666
"no-misused-new": true,

0 commit comments

Comments
 (0)