From 273d0ea217147d49c62678c834deb120ea35b008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anderson=20Greg=C3=B3rio?= Date: Thu, 10 Jul 2025 11:55:35 -0300 Subject: [PATCH] =?UTF-8?q?fix(components):=20ajusta=20valida=C3=A7=C3=A3o?= =?UTF-8?q?=20com=20p-control-value-with-label?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../po-combo/po-combo-base.component.spec.ts | 23 +++++++++++++++++++ .../po-combo/po-combo-base.component.ts | 7 +++++- .../po-field/po-field-validate.model.spec.ts | 23 +++++++++++++++++++ .../po-field/po-field-validate.model.ts | 14 ++++++++++- .../po-field/po-select/po-select.component.ts | 7 ------ 5 files changed, 65 insertions(+), 9 deletions(-) diff --git a/projects/ui/src/lib/components/po-field/po-combo/po-combo-base.component.spec.ts b/projects/ui/src/lib/components/po-field/po-combo/po-combo-base.component.spec.ts index bee4c4a24e..9a9444c786 100644 --- a/projects/ui/src/lib/components/po-field/po-combo/po-combo-base.component.spec.ts +++ b/projects/ui/src/lib/components/po-field/po-combo/po-combo-base.component.spec.ts @@ -896,6 +896,20 @@ describe('PoComboBaseComponent:', () => { expect(ValidatorsFunctions.requiredFailed).toHaveBeenCalled(); }); + it('validate: should return required obj when `requiredFailed` and `controlValeuWithLabel` is true.', () => { + const validObj = { + required: { + valid: false + } + }; + + spyOn(ValidatorsFunctions, 'requiredFailed').and.returnValue(true); + component.controlValueWithLabel = true; + + expect(component.validate(new FormControl({ value: 1, label: 'Label1' }))).toEqual(validObj); + expect(ValidatorsFunctions.requiredFailed).toHaveBeenCalled(); + }); + it('validate: should return undefined when `requiredFailed` is false', () => { spyOn(ValidatorsFunctions, 'requiredFailed').and.returnValue(false); @@ -903,6 +917,15 @@ describe('PoComboBaseComponent:', () => { expect(ValidatorsFunctions.requiredFailed).toHaveBeenCalled(); }); + it('validate: should return undefined when `requiredFailed` is false and `controlValueWithLabel` is true', () => { + spyOn(ValidatorsFunctions, 'requiredFailed').and.returnValue(false); + + component.controlValueWithLabel = true; + + expect(component.validate(new UntypedFormControl(null))).toBeUndefined(); + expect(ValidatorsFunctions.requiredFailed).toHaveBeenCalled(); + }); + it('validate: should set hasValidatorRequired to true if fieldErrorMessage is valid and control has required validator', () => { component['hasValidatorRequired'] = false; component.fieldErrorMessage = 'Field Invalid'; diff --git a/projects/ui/src/lib/components/po-field/po-combo/po-combo-base.component.ts b/projects/ui/src/lib/components/po-field/po-combo/po-combo-base.component.ts index 2581cae14f..6db60b869c 100644 --- a/projects/ui/src/lib/components/po-field/po-combo/po-combo-base.component.ts +++ b/projects/ui/src/lib/components/po-field/po-combo/po-combo-base.component.ts @@ -1041,11 +1041,16 @@ export abstract class PoComboBaseComponent implements ControlValueAccessor, OnIn } validate(abstractControl: AbstractControl): { [key: string]: any } { + let { value } = abstractControl; + if (this.controlValueWithLabel && value?.value) { + value = value.value; + } + if (!this.hasValidatorRequired && this.fieldErrorMessage && abstractControl.hasValidator(Validators.required)) { this.hasValidatorRequired = true; } - if (requiredFailed(this.required || this.hasValidatorRequired, this.disabled, abstractControl.value)) { + if (requiredFailed(this.required || this.hasValidatorRequired, this.disabled, value)) { this.changeDetector.markForCheck(); return { required: { diff --git a/projects/ui/src/lib/components/po-field/po-field-validate.model.spec.ts b/projects/ui/src/lib/components/po-field/po-field-validate.model.spec.ts index 02d0d2e156..943897fea6 100644 --- a/projects/ui/src/lib/components/po-field/po-field-validate.model.spec.ts +++ b/projects/ui/src/lib/components/po-field/po-field-validate.model.spec.ts @@ -77,10 +77,33 @@ describe('PoFieldValidateModel', () => { expect(ValidatorsFunctions.requiredFailed).toHaveBeenCalled(); }); + it('validate: should return required obj when `requiredFailed` and `controlValueWithLabel` is true.', () => { + const validObj = { + required: { + valid: false + } + }; + + spyOn(ValidatorsFunctions, 'requiredFailed').and.returnValue(true); + component.controlValueWithLabel = true; + + expect(component.validate(new FormControl({ value: 1, label: 'Label1' }))).toEqual(validObj); + expect(ValidatorsFunctions.requiredFailed).toHaveBeenCalled(); + }); + it('validate: should return undefined when `requiredFailed` is false', () => { spyOn(ValidatorsFunctions, 'requiredFailed').and.returnValue(false); expect(component.validate(new UntypedFormControl(null))).toBeNull(); expect(ValidatorsFunctions.requiredFailed).toHaveBeenCalled(); }); + + it('validate: should return undefined when `requiredFailed` is false and `controlValueWithLabel` is true', () => { + spyOn(ValidatorsFunctions, 'requiredFailed').and.returnValue(false); + + component.controlValueWithLabel = true; + + expect(component.validate(new UntypedFormControl(null))).toBeNull(); + expect(ValidatorsFunctions.requiredFailed).toHaveBeenCalled(); + }); }); diff --git a/projects/ui/src/lib/components/po-field/po-field-validate.model.ts b/projects/ui/src/lib/components/po-field/po-field-validate.model.ts index 058c3e0671..92fd30569d 100644 --- a/projects/ui/src/lib/components/po-field/po-field-validate.model.ts +++ b/projects/ui/src/lib/components/po-field/po-field-validate.model.ts @@ -11,6 +11,13 @@ import { requiredFailed } from './validators'; */ @Directive() export abstract class PoFieldValidateModel extends PoFieldModel implements Validator { + /** + * @docsPrivate + * + * Determinar se o valor do compo deve retorna objeto do tipo {value: any, label: any} + */ + @Input({ alias: 'p-control-value-with-label', transform: convertToBoolean }) controlValueWithLabel?: boolean = false; + /** * @optional * @@ -79,11 +86,16 @@ export abstract class PoFieldValidateModel extends PoFieldModel implements } validate(abstractControl: AbstractControl): ValidationErrors { + let { value } = abstractControl; + if (this.controlValueWithLabel && value?.value) { + value = value.value; + } + if (!this.hasValidatorRequired && this.fieldErrorMessage && abstractControl.hasValidator(Validators.required)) { this.hasValidatorRequired = true; } - if (requiredFailed(this.required || this.hasValidatorRequired, this.disabled, abstractControl.value)) { + if (requiredFailed(this.required || this.hasValidatorRequired, this.disabled, value)) { this.changeDetector.markForCheck(); return { required: { diff --git a/projects/ui/src/lib/components/po-field/po-select/po-select.component.ts b/projects/ui/src/lib/components/po-field/po-select/po-select.component.ts index 450998dbae..50bd1c0229 100644 --- a/projects/ui/src/lib/components/po-field/po-select/po-select.component.ts +++ b/projects/ui/src/lib/components/po-field/po-select/po-select.component.ts @@ -286,13 +286,6 @@ export class PoSelectComponent extends PoFieldValidateModel implements OnCh } } - /** - * @docsPrivate - * - * Determinar se o valor do compo deve retorna objeto do tipo {value: any, label: any} - */ - @Input({ alias: 'p-control-value-with-label', transform: convertToBoolean }) controlValueWithLabel?: boolean = false; - get fieldValue() { return this._fieldValue; }