packages/ui/src/lib/form/form.service.ts
This service is the interface between Angular Forms and ec.components core classes.
Properties |
|
Methods |
|
constructor(symbol: SymbolService)
|
||||||
Defined in packages/ui/src/lib/form/form.service.ts:9
|
||||||
Parameters :
|
Public addField |
addField(field: Field, form: Form
|
Defined in packages/ui/src/lib/form/form.service.ts:47
|
adds a new field to a form. handles form group and control
Returns :
void
|
getFormLabel | ||||||||||||
getFormLabel(form: FormComponent
|
||||||||||||
Defined in packages/ui/src/lib/form/form.service.ts:87
|
||||||||||||
Returns label for given form (e.g. Edit label)
Parameters :
Returns :
string
|
Public getGroup | ||||||
getGroup(form: Form
|
||||||
Defined in packages/ui/src/lib/form/form.service.ts:25
|
||||||
Initializes the form group from the form fields
Parameters :
Returns :
any
|
getValidators | ||||||
getValidators(field: Field)
|
||||||
Defined in packages/ui/src/lib/form/form.service.ts:55
|
||||||
Extracts all validators from a given Field instance.
Parameters :
Returns :
ValidatorFn[]
|
isReadOnly | ||||||
isReadOnly(field, form)
|
||||||
Defined in packages/ui/src/lib/form/form.service.ts:82
|
||||||
Returns true if the field should be readOnly, depending on its config and the form state.
Parameters :
Returns :
any
|
Public shouldBePartOfForm | ||||||
shouldBePartOfForm(field, form)
|
||||||
Defined in packages/ui/src/lib/form/form.service.ts:14
|
||||||
Returns true if the field should be included in the form. Decides based on field config values form, edit and create
Parameters :
Returns :
boolean
|
validateFactory | ||||||
validateFactory(field: Field)
|
||||||
Defined in packages/ui/src/lib/form/form.service.ts:67
|
||||||
Returns a Validation function from the given field (using field.validate)
Parameters :
Returns :
ValidationErrors | null
|
Public symbol |
Type : SymbolService
|
Defined in packages/ui/src/lib/form/form.service.ts:10
|
import { Injectable } from '@angular/core';
import { Form, Field } from '@ec.components/core';
import { AbstractControl, FormControl, FormGroup, ValidationErrors, ValidatorFn, Validators } from '@angular/forms';
import { SymbolService } from '../symbol/symbol.service';
import { FormComponent } from '../form/form.component';
/** This service is the interface between Angular Forms and ec.components core classes. */
@Injectable()
export class FormService {
constructor(public symbol: SymbolService) { }
/** Returns true if the field should be included in the form.
* Decides based on field config values form, edit and create */
public shouldBePartOfForm(field, form) {
if (field.create === false && !form.getBody()) {
return false;
}
if (field.edit === false && !!form.getBody()) {
return false;
}
return field.form !== false;
}
/** Initializes the form group from the form fields*/
public getGroup(form: Form<any>) {
const controls = {};
form.fields
.filter((field) => this.shouldBePartOfForm(field, form))
.forEach((field) => {
const validators = this.getValidators(field);
let value = form.getValue(field.property);
value = value === undefined ? null : value;
controls[field.property] = new FormControl(
{
value,
disabled: this.isReadOnly(field, form),
},
validators,
);
// TODO use { updateOn: blur } when updating to angular 5.0.0
// see https://github.com/angular/angular/commit/333a708bb632d4258ecb5fd4a0e86229fe9d26e4
});
return new FormGroup(controls);
}
/** adds a new field to a form. handles form group and control */
public addField(field: Field, form: Form<any>, group: FormGroup) {
console.warn('addField is experimental!');
const validators = this.getValidators(field);
const control = new FormControl({ value: form.getValue(field.property) }, validators);
group.addControl(field.property, control);
}
/** Extracts all validators from a given Field instance. */
getValidators(field: Field): ValidatorFn[] {
const validators = [];
if (field.required) {
validators.push(Validators.required);
}
if (field.validate) {
validators.push(this.validateFactory(field));
}
return validators;
}
/** Returns a Validation function from the given field (using field.validate) */
validateFactory(field: Field): ValidationErrors | null {
return (control: AbstractControl) => {
if (!field.validate) {
return;
}
const error = field.validate(control.value, field);
if (error) {
return {
custom: error,
};
}
};
}
/** Returns true if the field should be readOnly, depending on its config and the form state. */
isReadOnly(field, form) {
return field.disabled || (field.readOnly && form && !!form.getBody());
}
/** Returns label for given form (e.g. Edit label) */
getFormLabel(form: FormComponent<any>, label = this.symbol.resolve('resource.generic')) {
if (!form || !form.form) {
return '';
}
return `${this.symbol.resolve('resource.' + (form.form.isEditing() ? 'edit' : 'create'))}
${label} ${form.form.display() ? `"${form.form.display()}"` : ''}`;
}
}