Skip to content

Commit

Permalink
Merge pull request #2309 from ProgrammeVitam/cp_7.0_14026
Browse files Browse the repository at this point in the history
CP 7.0 - bug #14026 : agents services creation forbid only whitespace
  • Loading branch information
hazco75 authored Dec 13, 2024
2 parents ce26ba9 + 3cb5256 commit 7b2a029
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ <h5 i18n="Create agency title@@agencyCreateTitle2">Informations</h5>
i18n="Identfier exists error hint@@formErrorIdentifierExists"
>Identifiant déjà utilisé</vitamui-common-input-error
>
<vitamui-common-input-error *ngIf="!!form.get('identifier')?.errors?.onlyWhitespaces">{{
'COMMON.INVALID_FIELD' | translate
}}</vitamui-common-input-error>
</ng-container>
</vitamui-common-input>
</div>
Expand All @@ -49,6 +52,9 @@ <h5 i18n="Create agency title@@agencyCreateTitle2">Informations</h5>
<vitamui-common-input-error *ngIf="!!form?.get('name')?.errors?.nameExists" i18n="Name exists error hint@@formErrorNameExists"
>Nom déjà utilisé</vitamui-common-input-error
>
<vitamui-common-input-error *ngIf="!!form.get('name')?.errors?.onlyWhitespaces">{{
'COMMON.INVALID_FIELD' | translate
}}</vitamui-common-input-error>
</ng-container>
</vitamui-common-input>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ describe('AgencyCreateComponent', () => {
const agencyValidatorSpy = jasmine.createSpyObj('AgencyCreateValidators', {
uniqueIdentifier: () => of(null),
uniqueName: () => of(null),
onlyWhitespaces: () => {},
});
TestBed.configureTestingModule({
imports: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,12 @@ export class AgencyCreateComponent implements OnInit, OnDestroy {

ngOnInit() {
this.form = this.formBuilder.group({
name: [null, Validators.required],
identifier: [null, Validators.required, this.agencyCreateValidators.uniqueIdentifier()],
name: [null, [Validators.required, this.agencyCreateValidators.onlyWhitespaces]],
identifier: [
null,
[Validators.required, this.agencyCreateValidators.onlyWhitespaces],
this.agencyCreateValidators.uniqueIdentifier(),
],
description: [null],
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ describe('Agency Create Validators', () => {
expect(agencyServiceSpy.existsProperties).toHaveBeenCalledWith({ name: '123456' });
}));

it('should return { onlyWhitespaces: true }', fakeAsync(() => {
const agencyServiceSpy = jasmine.createSpyObj('AgencyService', ['']);
const agencyCreateValidators = new AgencyCreateValidators(agencyServiceSpy);
expect(agencyCreateValidators.onlyWhitespaces(new FormControl(' '))).toEqual({
onlyWhitespaces: true,
});
}));

it('should not call the service', fakeAsync(() => {
const agencyServiceSpy = jasmine.createSpyObj('AgencyService', ['existsProperties']);
agencyServiceSpy.existsProperties.and.returnValue(of(true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* knowledge of the CeCILL-C license and that you accept its terms.
*/
import { Injectable } from '@angular/core';
import { AbstractControl, AsyncValidatorFn } from '@angular/forms';
import { AbstractControl, AsyncValidatorFn, ValidatorFn } from '@angular/forms';

import { of, timer } from 'rxjs';
import { map, switchMap, take } from 'rxjs/operators';
Expand All @@ -55,6 +55,11 @@ export class AgencyCreateValidators {
return this.uniqueFields('identifier', 'identifierExists', identifierToIgnore);
};

onlyWhitespaces: ValidatorFn = (control: AbstractControl) => {
const regExp = /^\s+$/;
return regExp.test(control.value) ? { onlyWhitespaces: true } : null;
};

private uniqueFields(field: string, existTag: string, valueToIgnore?: string) {
return (control: AbstractControl) => {
const properties: any = {};
Expand Down
3 changes: 2 additions & 1 deletion ui/ui-frontend/projects/referential/src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"SUBMIT": "Confirm",
"UPDATE": "Update",
"APPLY_UPDATE": "Apply changes",
"REQUIRED": "Required field"
"REQUIRED": "Required field",
"INVALID_FIELD": "Invalid field"
},
"CONTRACT_MANAGEMENT": {
"CONTRACT_PAGE": {
Expand Down
3 changes: 2 additions & 1 deletion ui/ui-frontend/projects/referential/src/assets/i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"SUBMIT": "Confirmer",
"UPDATE": "Modifier",
"APPLY_UPDATE": "Appliquer les modifications ",
"REQUIRED": "Champ requis"
"REQUIRED": "Champ requis",
"INVALID_FIELD": "Champ invalide"
},
"CONTRACT_MANAGEMENT": {
"CONTRACT_PAGE": {
Expand Down

0 comments on commit 7b2a029

Please sign in to comment.