Skip to content

Commit c94d2fa

Browse files
samir-ayoubjhosefmarks
authored andcommitted
feat(dynamic-form): adiciona propriedade p-validate-fields
A nova propriedade permite ao desenvolvedor restringir o disparo do validate do form para apenas os campos desejados Fixes DTHFUI-4120
1 parent 7b1d7cb commit c94d2fa

File tree

8 files changed

+110
-14
lines changed

8 files changed

+110
-14
lines changed

projects/ui/src/lib/components/po-dynamic/po-dynamic-form/po-dynamic-form-base.component.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,17 @@ export class PoDynamicFormBaseComponent {
217217
* ```
218218
* [p-validate]="this.myFunction.bind(this)"
219219
* ```
220+
*
221+
* > Se houver uma lista de campos para validação definida em `p-validate-fields`, a propriedade `validate` só receberá o disparo para os campos equivalentes.
220222
*/
221223
@Input('p-validate') validate?: string | Function;
224+
225+
/**
226+
* @optional
227+
*
228+
* @description
229+
*
230+
* Lista que define os campos que irão disparar o validate do form.
231+
*/
232+
@Input('p-validate-fields') validateFields?: Array<string>;
222233
}

projects/ui/src/lib/components/po-dynamic/po-dynamic-form/po-dynamic-form-fields/po-dynamic-form-fields-base.component.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ describe('PoDynamicFormFieldsBaseComponent:', () => {
5151

5252
expectPropertiesValues(component, 'value', validValues, validValues);
5353
});
54+
55+
it('validateFields: should set `p-validate-fields` with `[]` if it is an invalid Array type value', () => {
56+
const invalidValues = [undefined, null, '', true, false, 0, 1, 'string', {}];
57+
58+
expectPropertiesValues(component, 'validateFields', invalidValues, []);
59+
});
60+
61+
it('validateFields: should update property `p-validate-fields` with valid values', () => {
62+
const validValues = [['propertyA'], ['propertyB']];
63+
64+
expectPropertiesValues(component, 'validateFields', validValues, validValues);
65+
});
5466
});
5567

5668
describe('Methods:', () => {

projects/ui/src/lib/components/po-dynamic/po-dynamic-form/po-dynamic-form-fields/po-dynamic-form-fields-base.component.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { PoDynamicFormFieldInternal } from './po-dynamic-form-field-internal.int
1111
@Directive()
1212
export class PoDynamicFormFieldsBaseComponent {
1313
private _fields: Array<PoDynamicFormField>;
14+
private _validateFields: Array<string>;
1415
private _value?: any = {};
1516

1617
visibleFields: Array<PoDynamicFormFieldInternal> = [];
@@ -41,6 +42,14 @@ export class PoDynamicFormFieldsBaseComponent {
4142

4243
@Input('p-validate') validate?: string | Function;
4344

45+
@Input('p-validate-fields') set validateFields(value: Array<string>) {
46+
this._validateFields = Array.isArray(value) ? [...value] : [];
47+
}
48+
49+
get validateFields() {
50+
return this._validateFields;
51+
}
52+
4453
@Output('p-form-validate') formValidate = new EventEmitter<any>();
4554

4655
constructor(private titleCasePipe: TitleCasePipe) {}

projects/ui/src/lib/components/po-dynamic/po-dynamic-form/po-dynamic-form-fields/po-dynamic-form-fields.component.spec.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,62 @@ describe('PoDynamicFormFieldsComponent: ', () => {
168168
expect(component['previousValue']).toEqual(newValue);
169169
});
170170

171+
it('should emit `formValidate` if the changed field is included in `validateFields`', async () => {
172+
const fakeVisibleField = { property: 'test1' };
173+
const field = { changedField: { property: 'test1' }, changedFieldIndex: 0 };
174+
175+
component.formValidate.observers.length = 1;
176+
component.validate = 'http://fakeUrlPo.com';
177+
component.fields = [{ property: 'test1', validate: 'teste' }];
178+
component['previousValue']['test1'] = 'value';
179+
component['value']['test1'] = 'new value';
180+
component.validateFields = ['test1'];
181+
182+
spyOn(component, <any>'getField').and.returnValue(field);
183+
const spyEmit = spyOn(component.formValidate, 'emit');
184+
185+
await component.onChangeField(fakeVisibleField);
186+
187+
expect(spyEmit).toHaveBeenCalledWith(component.fields[0]);
188+
});
189+
190+
it('should emit `formValidate` if `validateFields` isn`t defined', async () => {
191+
const fakeVisibleField = { property: 'test1' };
192+
const field = { changedField: { property: 'test1' }, changedFieldIndex: 0 };
193+
194+
component.formValidate.observers.length = 1;
195+
component.validate = 'http://fakeUrlPo.com';
196+
component.fields = [{ property: 'test1', validate: 'teste' }];
197+
component['previousValue']['test1'] = 'value';
198+
component['value']['test1'] = 'new value';
199+
200+
spyOn(component, <any>'getField').and.returnValue(field);
201+
const spyEmit = spyOn(component.formValidate, 'emit');
202+
203+
await component.onChangeField(fakeVisibleField);
204+
205+
expect(spyEmit).toHaveBeenCalledWith(component.fields[0]);
206+
});
207+
208+
it('shouldn`t emit `formValidate` if the changed field isn`t included in `validateFields`', async () => {
209+
const fakeVisibleField = { property: 'test1' };
210+
const field = { changedField: { property: 'test1' }, changedFieldIndex: 0 };
211+
212+
component.formValidate.observers.length = 1;
213+
component.validate = 'http://fakeUrlPo.com';
214+
component.fields = [{ property: 'test1', validate: 'teste' }];
215+
component['previousValue']['test1'] = 'value';
216+
component['value']['test1'] = 'new value';
217+
component.validateFields = ['test2', 'test3'];
218+
219+
spyOn(component, <any>'getField').and.returnValue(field);
220+
const spyEmit = spyOn(component.formValidate, 'emit');
221+
222+
await component.onChangeField(fakeVisibleField);
223+
224+
expect(spyEmit).not.toHaveBeenCalled();
225+
});
226+
171227
it('shouldn`t call `validateField` if `changedField.validate` doesn`t have value', async () => {
172228
const fakeVisibleField = { property: 'test1' };
173229

projects/ui/src/lib/components/po-dynamic/po-dynamic-form/po-dynamic-form-fields/po-dynamic-form-fields.component.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ export class PoDynamicFormFieldsComponent extends PoDynamicFormFieldsBaseCompone
100100
}
101101

102102
private triggerValidationOnForm(changedFieldIndex: number) {
103-
const hasValidationForm = this.validate && this.formValidate.observers.length;
103+
const isValidatableField = this.validateFields?.length
104+
? this.validateFieldsChecker(this.validateFields, this.fields[changedFieldIndex].property)
105+
: true;
106+
const hasValidationForm = this.validate && isValidatableField && this.formValidate.observers.length;
104107

105108
if (hasValidationForm) {
106109
const updatedField = this.fields[changedFieldIndex];
@@ -113,6 +116,10 @@ export class PoDynamicFormFieldsComponent extends PoDynamicFormFieldsBaseCompone
113116
this.visibleFields = this.getVisibleFields();
114117
}
115118

119+
private validateFieldsChecker(validateFields: Array<string>, propertyField: PoDynamicFormField['property']): boolean {
120+
return validateFields.some(validateFieldItem => validateFieldItem === propertyField);
121+
}
122+
116123
private async validateField(field: PoDynamicFormField, fieldIndex: number, visibleField: PoDynamicFormField) {
117124
const value = this.value[field.property];
118125

projects/ui/src/lib/components/po-dynamic/po-dynamic-form/po-dynamic-form.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
[p-auto-focus]="autoFocus"
1414
[p-disabled-form]="disabledForm"
1515
[p-validate]="validate"
16+
[p-validate-fields]="validateFields"
1617
[p-value]="value"
1718
(p-form-validate)="validateForm($event)"
1819
>

projects/ui/src/lib/components/po-dynamic/po-dynamic-form/samples/sample-po-dynamic-form-register/sample-po-dynamic-form-register.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[p-fields]="fields"
55
[p-load]="onLoadFields.bind(this)"
66
[p-validate]="this.onChangeFields.bind(this)"
7+
[p-validate-fields]="validateFields"
78
[p-value]="person"
89
>
910
</po-dynamic-form>

projects/ui/src/lib/components/po-dynamic/po-dynamic-form/samples/sample-po-dynamic-form-register/sample-po-dynamic-form-register.component.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { PoDynamicFormRegisterService } from './sample-po-dynamic-form-register.
1515
})
1616
export class SamplePoDynamicFormRegisterComponent implements OnInit {
1717
person = {};
18+
validateFields: Array<string> = ['state'];
1819

1920
fields: Array<PoDynamicFormField> = [
2021
{
@@ -111,19 +112,17 @@ export class SamplePoDynamicFormRegisterComponent implements OnInit {
111112
}
112113

113114
onChangeFields(changedValue: PoDynamicFormFieldChanged): PoDynamicFormValidation {
114-
if (changedValue.property === 'state') {
115-
return {
116-
value: { city: undefined },
117-
fields: [
118-
{
119-
property: 'city',
120-
gridColumns: 6,
121-
options: this.registerService.getCity(changedValue.value.state),
122-
disabled: false
123-
}
124-
]
125-
};
126-
}
115+
return {
116+
value: { city: undefined },
117+
fields: [
118+
{
119+
property: 'city',
120+
gridColumns: 6,
121+
options: this.registerService.getCity(changedValue.value.state),
122+
disabled: false
123+
}
124+
]
125+
};
127126
}
128127

129128
onLoadFields(value: any) {

0 commit comments

Comments
 (0)