From 71a1ca1787875801c2de3cdaf0b9266af8fb3c64 Mon Sep 17 00:00:00 2001 From: Vito Date: Tue, 13 Feb 2018 10:10:13 +0000 Subject: [PATCH] [ADF-2225] visibility and validation must be done after form is parsed (#2931) * [ADF-2225] visibility and validation must be done after form is parsed * [ADF-2225] remove fdescribe * [ADF-2225] fixed broken test * [ADF-2225] moved check visibility appended to the main condition * [ADF-2225] added visibility check to new validators --- .../form/components/form.component.spec.ts | 11 ++++++++ lib/core/form/components/form.component.ts | 5 ++++ .../components/start-form.component.spec.ts | 11 +++++++- .../form/components/start-form.component.ts | 4 +++ .../widgets/core/form-field-validator.ts | 26 +++++++++---------- 5 files changed, 43 insertions(+), 14 deletions(-) diff --git a/lib/core/form/components/form.component.spec.ts b/lib/core/form/components/form.component.spec.ts index b2a22b8260..c4e894b3e1 100644 --- a/lib/core/form/components/form.component.spec.ts +++ b/lib/core/form/components/form.component.spec.ts @@ -225,6 +225,17 @@ describe('FormComponent', () => { expect(formComponent.getFormDefinitionByFormId).toHaveBeenCalledWith(formId); }); + it('should refresh visibility when the form is loaded', () => { + spyOn(formService, 'getFormDefinitionById').and.returnValue(Observable.of(fakeForm)); + const formId = '123'; + + formComponent.formId = formId; + formComponent.loadForm(); + + expect(formService.getFormDefinitionById).toHaveBeenCalledWith(formId); + expect(visibilityService.refreshVisibility).toHaveBeenCalled(); + }); + it('should get form definition by form name on load', () => { spyOn(formComponent, 'getFormDefinitionByFormName').and.stub(); const formName = '
'; diff --git a/lib/core/form/components/form.component.ts b/lib/core/form/components/form.component.ts index 778fbabf7b..a3fc6611f8 100644 --- a/lib/core/form/components/form.component.ts +++ b/lib/core/form/components/form.component.ts @@ -344,6 +344,7 @@ export class FormComponent implements OnInit, OnChanges { form => { const parsedForm = this.parseForm(form); this.visibilityService.refreshVisibility(parsedForm); + parsedForm.validateForm(); this.form = parsedForm; this.onFormLoaded(this.form); resolve(this.form); @@ -365,6 +366,8 @@ export class FormComponent implements OnInit, OnChanges { form => { this.formName = form.name; this.form = this.parseForm(form); + this.visibilityService.refreshVisibility(this.form); + this.form.validateForm(); this.onFormLoaded(this.form); }, (error) => { @@ -381,6 +384,8 @@ export class FormComponent implements OnInit, OnChanges { this.formService.getFormDefinitionById(id).subscribe( form => { this.form = this.parseForm(form); + this.visibilityService.refreshVisibility(this.form); + this.form.validateForm(); this.onFormLoaded(this.form); }, (error) => { diff --git a/lib/core/form/components/start-form.component.spec.ts b/lib/core/form/components/start-form.component.spec.ts index 4cb2caddba..4e4a7a3970 100644 --- a/lib/core/form/components/start-form.component.spec.ts +++ b/lib/core/form/components/start-form.component.spec.ts @@ -39,6 +39,7 @@ describe('ActivitiStartForm', () => { let component: StartFormComponent; let fixture: ComponentFixture; let getStartFormSpy: jasmine.Spy; + let visibilityService: WidgetVisibilityService; const exampleId1 = 'my:process1'; const exampleId2 = 'my:process2'; @@ -71,11 +72,11 @@ describe('ActivitiStartForm', () => { fixture = TestBed.createComponent(StartFormComponent); component = fixture.componentInstance; formService = fixture.debugElement.injector.get(FormService); + visibilityService = TestBed.get(WidgetVisibilityService); getStartFormSpy = spyOn(formService, 'getStartFormDefinition').and.returnValue(Observable.of({ processDefinitionName: 'my:process' })); - }); it('should load start form on change if processDefinitionId defined', () => { @@ -90,6 +91,14 @@ describe('ActivitiStartForm', () => { expect(formService.getStartFormDefinition).toHaveBeenCalled(); }); + it('should check visibility when the start form is loaded', () => { + spyOn(visibilityService, 'refreshVisibility'); + component.processDefinitionId = exampleId1; + component.ngOnChanges({ processDefinitionId: new SimpleChange(exampleId1, exampleId2, true) }); + expect(formService.getStartFormDefinition).toHaveBeenCalled(); + expect(visibilityService.refreshVisibility).toHaveBeenCalled(); + }); + it('should not load start form when changes notified but no change to processDefinitionId', () => { component.processDefinitionId = exampleId1; component.ngOnChanges({ otherProp: new SimpleChange(exampleId1, exampleId2, true) }); diff --git a/lib/core/form/components/start-form.component.ts b/lib/core/form/components/start-form.component.ts index fba4d3d36f..e38bf3fd32 100644 --- a/lib/core/form/components/start-form.component.ts +++ b/lib/core/form/components/start-form.component.ts @@ -120,6 +120,8 @@ export class StartFormComponent extends FormComponent implements OnChanges, OnIn form.processVariables = intance.variables; } this.form = this.parseForm(form); + this.visibilityService.refreshVisibility(this.form); + this.form.validateForm(); this.form.readOnly = this.readOnlyForm; this.onFormLoaded(this.form); }, @@ -135,6 +137,8 @@ export class StartFormComponent extends FormComponent implements OnChanges, OnIn form => { this.formName = form.processDefinitionName; this.form = this.parseForm(form); + this.visibilityService.refreshVisibility(this.form); + this.form.validateForm(); this.form.readOnly = this.readOnlyForm; this.onFormLoaded(this.form); }, diff --git a/lib/core/form/components/widgets/core/form-field-validator.ts b/lib/core/form/components/widgets/core/form-field-validator.ts index 93a251143f..754965f770 100644 --- a/lib/core/form/components/widgets/core/form-field-validator.ts +++ b/lib/core/form/components/widgets/core/form-field-validator.ts @@ -53,7 +53,7 @@ export class RequiredFieldValidator implements FormFieldValidator { } validate(field: FormFieldModel): boolean { - if (this.isSupported(field)) { + if (this.isSupported(field) && field.isVisible) { if (field.type === FormFieldTypes.DROPDOWN) { if (field.hasEmptyValue && field.emptyOption) { @@ -105,7 +105,7 @@ export class NumberFieldValidator implements FormFieldValidator { } validate(field: FormFieldModel): boolean { - if (this.isSupported(field)) { + if (this.isSupported(field) && field.isVisible) { if (field.value === null || field.value === undefined || field.value === '') { @@ -147,7 +147,7 @@ export class DateFieldValidator implements FormFieldValidator { } validate(field: FormFieldModel): boolean { - if (this.isSupported(field) && field.value) { + if (this.isSupported(field) && field.value && field.isVisible) { if (DateFieldValidator.isValidDate(field.value, field.dateDisplayFormat)) { return true; } @@ -171,7 +171,7 @@ export class MinDateFieldValidator implements FormFieldValidator { validate(field: FormFieldModel): boolean { let isValid = true; - if (this.isSupported(field) && field.value) { + if (this.isSupported(field) && field.value && field.isVisible) { const dateFormat = field.dateDisplayFormat; if (!DateFieldValidator.isValidDate(field.value, dateFormat)) { @@ -219,7 +219,7 @@ export class MaxDateFieldValidator implements FormFieldValidator { } validate(field: FormFieldModel): boolean { - if (this.isSupported(field) && field.value) { + if (this.isSupported(field) && field.value && field.isVisible) { const dateFormat = field.dateDisplayFormat; if (!DateFieldValidator.isValidDate(field.value, dateFormat)) { @@ -259,7 +259,7 @@ export class MinDateTimeFieldValidator implements FormFieldValidator { validate(field: FormFieldModel): boolean { let isValid = true; - if (this.isSupported(field) && field.value) { + if (this.isSupported(field) && field.value && field.isVisible) { const dateFormat = field.dateDisplayFormat; if (!DateFieldValidator.isValidDate(field.value, dateFormat)) { @@ -306,7 +306,7 @@ export class MaxDateTimeFieldValidator implements FormFieldValidator { validate(field: FormFieldModel): boolean { let isValid = true; - if (this.isSupported(field) && field.value) { + if (this.isSupported(field) && field.value && field.isVisible) { const dateFormat = field.dateDisplayFormat; if (!DateFieldValidator.isValidDate(field.value, dateFormat)) { @@ -354,7 +354,7 @@ export class MinLengthFieldValidator implements FormFieldValidator { } validate(field: FormFieldModel): boolean { - if (this.isSupported(field) && field.value) { + if (this.isSupported(field) && field.value && field.isVisible) { if (field.value.length >= field.minLength) { return true; } @@ -380,7 +380,7 @@ export class MaxLengthFieldValidator implements FormFieldValidator { } validate(field: FormFieldModel): boolean { - if (this.isSupported(field) && field.value) { + if (this.isSupported(field) && field.value && field.isVisible) { if (field.value.length <= field.maxLength) { return true; } @@ -406,7 +406,7 @@ export class MinValueFieldValidator implements FormFieldValidator { } validate(field: FormFieldModel): boolean { - if (this.isSupported(field) && field.value) { + if (this.isSupported(field) && field.value && field.isVisible) { let value: number = +field.value; let minValue: number = +field.minValue; @@ -436,7 +436,7 @@ export class MaxValueFieldValidator implements FormFieldValidator { } validate(field: FormFieldModel): boolean { - if (this.isSupported(field) && field.value) { + if (this.isSupported(field) && field.value && field.isVisible) { let value: number = +field.value; let maxValue: number = +field.maxValue; @@ -465,7 +465,7 @@ export class RegExFieldValidator implements FormFieldValidator { } validate(field: FormFieldModel): boolean { - if (this.isSupported(field) && field.value) { + if (this.isSupported(field) && field.value && field.isVisible) { if (field.value.length > 0 && field.value.match(new RegExp('^' + field.regexPattern + '$'))) { return true; } @@ -508,7 +508,7 @@ export class FixedValueFieldValidator implements FormFieldValidator { } validate(field: FormFieldModel): boolean { - if (this.isSupported(field)) { + if (this.isSupported(field) && field.isVisible) { if (this.hasStringValue(field) && this.hasOptions(field) && !this.hasValidNameOrValidId(field)) { field.validationSummary.message = 'FORM.FIELD.VALIDATOR.INVALID_VALUE'; return false;