Skip to content

Commit

Permalink
feat: years validator
Browse files Browse the repository at this point in the history
  • Loading branch information
69pmb committed Jan 20, 2024
1 parent d22a195 commit 818c388
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 24 deletions.
9 changes: 9 additions & 0 deletions src/app/list-composition/list-composition.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@
placeholder="Année de fin"
></app-filter-year>
</div>
<div
*ngIf="
filters.errors?.['invalidYears'] &&
(filters.touched || filters.dirty)
"
class="mat-mdc-form-field-error years-error"
>
L'année de fin doit être supérieure à l'année de début
</div>
</div>
<fa-icon
class="reset-btn pointer"
Expand Down
38 changes: 24 additions & 14 deletions src/app/list-composition/list-composition.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {UtilsService} from '@services/utils.service';
import {ListDirective} from '../list/list.component';
import {DexieService} from '@services/dexie.service';
import {Dropbox} from '@utils/dropbox';
import {yearsValidator} from '@utils/year.validator';

@Component({
selector: 'app-list-composition',
Expand Down Expand Up @@ -65,20 +66,29 @@ export class ListCompositionComponent
category: FormControl<string[] | undefined>;
begin: FormControl<number | undefined>;
end: FormControl<number | undefined>;
}>({
artist: new FormControl<string | undefined>(undefined, {nonNullable: true}),
title: new FormControl<string | undefined>(undefined, {nonNullable: true}),
filename: new FormControl<string | undefined>(undefined, {
nonNullable: true,
}),
type: new FormControl<string | undefined>(undefined, {nonNullable: true}),
deleted: new FormControl<boolean>(false, {nonNullable: true}),
category: new FormControl<string[] | undefined>(undefined, {
nonNullable: true,
}),
begin: new FormControl<number | undefined>(undefined, {nonNullable: true}),
end: new FormControl<number | undefined>(undefined, {nonNullable: true}),
});
}>(
{
artist: new FormControl<string | undefined>(undefined, {
nonNullable: true,
}),
title: new FormControl<string | undefined>(undefined, {
nonNullable: true,
}),
filename: new FormControl<string | undefined>(undefined, {
nonNullable: true,
}),
type: new FormControl<string | undefined>(undefined, {nonNullable: true}),
deleted: new FormControl<boolean>(false, {nonNullable: true}),
category: new FormControl<string[] | undefined>(undefined, {
nonNullable: true,
}),
begin: new FormControl<number | undefined>(undefined, {
nonNullable: true,
}),
end: new FormControl<number | undefined>(undefined, {nonNullable: true}),
},
{validators: yearsValidator}
);

constructor(
private myCompositionsService: DataService<Composition>,
Expand Down
8 changes: 8 additions & 0 deletions src/app/list-fichier/list-fichier.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
<mat-checkbox [formControl]="filters.controls.top"> </mat-checkbox>
</span>
</div>
<div
*ngIf="
filters.errors?.['invalidYears'] && (filters.touched || filters.dirty)
"
class="mat-mdc-form-field-error years-error"
>
L'année de fin doit être supérieure à l'année de début
</div>
</div>
<fa-icon
class="reset-btn pointer"
Expand Down
24 changes: 14 additions & 10 deletions src/app/list-fichier/list-fichier.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {DexieService} from '@services/dexie.service';
import {Utils} from '@utils/utils';
import {Dropbox} from '@utils/dropbox';
import {FormGroup, FormControl} from '@angular/forms';
import {yearsValidator} from '@utils/year.validator';

@Component({
selector: 'app-list-fichier',
Expand Down Expand Up @@ -59,16 +60,19 @@ export class ListFichierComponent
top: FormControl<boolean>;
begin: FormControl<number | undefined>;
end: FormControl<number | undefined>;
}>({
author: new FormControl(),
name: new FormControl(),
type: new FormControl(),
deleted: new FormControl(),
category: new FormControl(),
top: new FormControl(),
begin: new FormControl(),
end: new FormControl(),
});
}>(
{
author: new FormControl(),
name: new FormControl(),
type: new FormControl(),
deleted: new FormControl(),
category: new FormControl(),
top: new FormControl(),
begin: new FormControl(),
end: new FormControl(),
},
{validators: yearsValidator}
);

constructor(
private myFichiersService: DataService<Fichier>,
Expand Down
13 changes: 13 additions & 0 deletions src/app/list/list.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
}
}

.years-error {
float: right;
width: 424px;
display: flex;
justify-content: center;
padding-bottom: 15px;
}

::ng-deep.mat-mdc-paginator-container {
justify-content: center !important;
}
Expand Down Expand Up @@ -130,6 +138,11 @@
flex-direction: column;
}

.years-error {
float: unset;
justify-content: start;
}

.reset-btn {
margin-bottom: 22px;
}
Expand Down
19 changes: 19 additions & 0 deletions src/app/utils/year.validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {AbstractControl, ValidationErrors, ValidatorFn} from '@angular/forms';

/** Begin year must be lower than end year */
export const yearsValidator: ValidatorFn = (
control: AbstractControl
): ValidationErrors | null => {
const begin = control.get('begin');
const end = control.get('end');

return begin?.value &&
end?.value &&
!begin.pristine &&
!end.pristine &&
!begin.errors &&
!end.errors &&
begin.value > end.value
? {invalidYears: true}
: null;
};

0 comments on commit 818c388

Please sign in to comment.