Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -896,13 +896,36 @@ 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);

expect(component.validate(new UntypedFormControl(null))).toBeUndefined();
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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import { requiredFailed } from './validators';
*/
@Directive()
export abstract class PoFieldValidateModel<T> extends PoFieldModel<T> 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
*
Expand Down Expand Up @@ -79,11 +86,16 @@ export abstract class PoFieldValidateModel<T> extends PoFieldModel<T> 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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,6 @@ export class PoSelectComponent extends PoFieldValidateModel<any> 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;
}
Expand Down