mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
AAE-30864 Refactored services to accept injected validators (#10660)
* [AAE-30864] refactored services to accept injected validators * [AAE-30864] updated documentation, applied pr comments
This commit is contained in:
@@ -1686,3 +1686,9 @@ export const mockFormWithSections = {
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const fakeValidatorMock = {
|
||||
supportedTypes: ['test'],
|
||||
isSupported: () => true,
|
||||
validate: () => true
|
||||
};
|
||||
|
@@ -24,7 +24,7 @@ import { FormFieldModel } from './form-field.model';
|
||||
import { FormOutcomeModel } from './form-outcome.model';
|
||||
import { FormModel } from './form.model';
|
||||
import { TabModel } from './tab.model';
|
||||
import { fakeMetadataForm, mockDisplayExternalPropertyForm, mockFormWithSections } from '../../mock/form.mock';
|
||||
import { fakeMetadataForm, mockDisplayExternalPropertyForm, mockFormWithSections, fakeValidatorMock } from '../../mock/form.mock';
|
||||
import { CoreTestingModule } from '../../../../testing';
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
@@ -433,6 +433,13 @@ describe('FormModel', () => {
|
||||
expect(FORM_FIELD_VALIDATORS.length).toBe(defaultLength);
|
||||
});
|
||||
|
||||
it('should include injected field validators', () => {
|
||||
const form = new FormModel({}, null, false, formService, undefined, [fakeValidatorMock]);
|
||||
const defaultLength = FORM_FIELD_VALIDATORS.length;
|
||||
|
||||
expect(form.fieldValidators.length).toBe(defaultLength + 1);
|
||||
});
|
||||
|
||||
describe('variables', () => {
|
||||
let form: FormModel;
|
||||
|
||||
|
@@ -85,7 +85,7 @@ export class FormModel implements ProcessFormModel {
|
||||
tabs: TabModel[] = [];
|
||||
fields: (ContainerModel | FormFieldModel)[] = [];
|
||||
outcomes: FormOutcomeModel[] = [];
|
||||
fieldValidators: FormFieldValidator[] = [...FORM_FIELD_VALIDATORS];
|
||||
fieldValidators: FormFieldValidator[] = [];
|
||||
customFieldTemplates: FormFieldTemplates = {};
|
||||
theme?: ThemeModel;
|
||||
|
||||
@@ -100,7 +100,8 @@ export class FormModel implements ProcessFormModel {
|
||||
formValues?: FormValues,
|
||||
readOnly: boolean = false,
|
||||
protected formService?: FormValidationService,
|
||||
enableFixedSpace?: boolean
|
||||
enableFixedSpace?: boolean,
|
||||
injectedFieldValidators?: FormFieldValidator[]
|
||||
) {
|
||||
this.readOnly = readOnly;
|
||||
this.json = json;
|
||||
@@ -133,6 +134,7 @@ export class FormModel implements ProcessFormModel {
|
||||
this.parseOutcomes();
|
||||
}
|
||||
|
||||
this.loadInjectedFieldValidators(injectedFieldValidators);
|
||||
this.validateForm();
|
||||
}
|
||||
|
||||
@@ -501,4 +503,8 @@ export class FormModel implements ProcessFormModel {
|
||||
variable.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
private loadInjectedFieldValidators(injectedFieldValidators: FormFieldValidator[]): void {
|
||||
this.fieldValidators = injectedFieldValidators ? [...FORM_FIELD_VALIDATORS, ...injectedFieldValidators] : [...FORM_FIELD_VALIDATORS];
|
||||
}
|
||||
}
|
||||
|
@@ -17,15 +17,23 @@
|
||||
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { formModelTabs } from '../../mock';
|
||||
import { FormService } from './form.service';
|
||||
import { FORM_SERVICE_FIELD_VALIDATORS_TOKEN, FormService } from './form.service';
|
||||
import { CoreTestingModule } from '../../testing';
|
||||
import { FORM_FIELD_VALIDATORS, FormFieldValidator } from '../public-api';
|
||||
|
||||
const fakeValidator = {
|
||||
supportedTypes: ['test'],
|
||||
isSupported: () => true,
|
||||
validate: () => true
|
||||
} as FormFieldValidator;
|
||||
|
||||
describe('Form service', () => {
|
||||
let service: FormService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [CoreTestingModule]
|
||||
imports: [CoreTestingModule],
|
||||
providers: [{ provide: FORM_SERVICE_FIELD_VALIDATORS_TOKEN, useValue: [fakeValidator] }]
|
||||
});
|
||||
service = TestBed.inject(FormService);
|
||||
});
|
||||
@@ -36,5 +44,11 @@ describe('Form service', () => {
|
||||
const formParsed = service.parseForm(formModelTabs);
|
||||
expect(formParsed).toBeDefined();
|
||||
});
|
||||
|
||||
it('should return form with injected field validators', () => {
|
||||
expect(formModelTabs.formRepresentation.formDefinition).toBeDefined();
|
||||
const formParsed = service.parseForm(formModelTabs);
|
||||
expect(formParsed.fieldValidators).toEqual([...FORM_FIELD_VALIDATORS, fakeValidator]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@@ -15,7 +15,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Inject, Injectable, InjectionToken, Optional } from '@angular/core';
|
||||
import { Subject } from 'rxjs';
|
||||
import { ContentLinkModel } from '../components/widgets/core/content-link.model';
|
||||
import { FormOutcomeEvent } from '../components/widgets/core/form-outcome-event.model';
|
||||
@@ -30,12 +30,15 @@ import { ValidateFormFieldEvent } from '../events/validate-form-field.event';
|
||||
import { FormValidationService } from './form-validation-service.interface';
|
||||
import { FormRulesEvent } from '../events/form-rules.event';
|
||||
import { FormSpinnerEvent } from '../events';
|
||||
import { FormFieldModel } from '../components/widgets';
|
||||
import { FormFieldModel, FormFieldValidator } from '../components/widgets';
|
||||
|
||||
export const FORM_SERVICE_FIELD_VALIDATORS_TOKEN = new InjectionToken<FormFieldValidator[]>('FORM_SERVICE_FIELD_VALIDATORS_TOKEN');
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class FormService implements FormValidationService {
|
||||
private fieldValidators: FormFieldValidator[];
|
||||
formLoaded = new Subject<FormEvent>();
|
||||
formDataRefreshed = new Subject<FormEvent>();
|
||||
formFieldValueChanged = new Subject<FormFieldEvent>();
|
||||
@@ -59,7 +62,9 @@ export class FormService implements FormValidationService {
|
||||
|
||||
formRulesEvent = new Subject<FormRulesEvent>();
|
||||
|
||||
constructor() {}
|
||||
constructor(@Optional() @Inject(FORM_SERVICE_FIELD_VALIDATORS_TOKEN) injectedFieldValidators?: FormFieldValidator[]) {
|
||||
this.fieldValidators = injectedFieldValidators || [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses JSON data to create a corresponding Form model.
|
||||
@@ -72,7 +77,7 @@ export class FormService implements FormValidationService {
|
||||
*/
|
||||
parseForm(json: any, data?: FormValues, readOnly: boolean = false, fixedSpace?: boolean): FormModel {
|
||||
if (json) {
|
||||
const form = new FormModel(json, data, readOnly, this, fixedSpace);
|
||||
const form = new FormModel(json, data, readOnly, this, fixedSpace, this.fieldValidators);
|
||||
if (!json.fields) {
|
||||
form.outcomes = [
|
||||
new FormOutcomeModel(form, {
|
||||
|
Reference in New Issue
Block a user