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 bee4c4a24..9a9444c78 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 2581cae14..6db60b869 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 02d0d2e15..943897fea 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 058c3e067..92fd30569 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 450998dba..50bd1c022 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; }