Form service

Usage Example

Constructor

  const formService = new UIKernel.Form();

Methods


initForm

  formService.initForm(settings);

Initialize a form

Parameters:

Type Name Description
String [ ] settings.fields Required. Fields list, that are required to display
FormModel settings.model Required. Form model
Object settings.data Optional. Preset data
Object settings.changes Optional. Preset changes
Boolean settings.submitAll=false Optional. Send all form for validity check
Boolean settings.partialErrorChecking=false Optional. Activate partial gradual form validation
Boolean settings.showDependentFields=false Optional. Mark the fields which are involved in group validation

addChangeListener

  formService.addChangeListener(handler);

Subscribe on formService ‘update’ events.

Parameters:

Type Name Description
Function handler Required. An event handler which receives changes object

handlers arguments:

Type Name Description
Object changes.data Form data, or null if it’s absent
Object changes.originalData Form data without changes
Boolean changes.isLoaded Check if data loaded
Object changes.changes Form changes
Object changes.errors Form errors
Boolean changes.submitting Check if form is being submitted

removeChangeListener

  formService.removeChangeListener(handler);

Unsubscribe from formService ‘update’ events.

Parameters:

Type Name Description
Function handler Required. An event handler to stop listening to events

removeAllListeners

  formService.removeAllListeners();

Unsubscribe all formService ‘update’ events handlers.


async updateField

  await formService.updateField(field, value);

Update changes of the specified form field(s). Returns promise which resolves when the field is updated. The method triggers ‘update’ event eventually

Parameters:

Type Name Description
String field Required. Field name to update.
Any value Required. Value to update the field with. Can be either an event or any serializable data

async validateField

  let validationErrors = await formService.validateField(field, value);

Update field value and validate form. Returns Promise which resolves with validation errors (if there are some invalid fields) or empty result. The method triggers ‘update’ event eventually

Parameters:

Type Name Description
String field Required. Field name to update.
Any value Required. Value to update the field with. Can be either an event or any serializable data

async validateForm

 let validationErrors = await formService.validateForm();

Validate form. Returns Promise which resolves with validation errors (if there are some invalid fields) or empty result. The method triggers ‘update’ event eventually


async set

  await formService.set(data, validate);

Set data(changes) in the form. Returns Promise which resolves after data is set. If parameter validate is specified, the promise will resolve with validation errors (if there are some invalid fields) or empty result. The method triggers ‘update’ event eventually

Parameters:

Type Name Description
Object data Required. New data to be saved in the internal class structure
Boolean validate=false Optional. If the data should be validated after saving(there might be kept invalid data and it’s ok)

async submit

  await formService.submit();

Send form data(changes) to the model. If the data is invalid, or there is appeared an error while sending form data to the model - the error will be thrown. If everything went well, the sent data will be returned. The method triggers ‘update’ event eventually


async submitData

  await formService.submitData(data);

Set data as changes in the form and send it to the model(combines set and submit methods). The method triggers ‘update’ event eventually

Parameters:

Type Name Description
Object data Required. New data to be saved in the internal class structure and sent to the model

clearvalidation

  formService.clearValidation(field);

Clear field error and warning mark. The method triggers ‘update’ event eventually

Parameters:

Type Name Description
String [ ] || String field Required. Field name(s) to clear error of. Accepts ether one string value, or array of string field names

clearFieldChanges

  formService.clearFieldChanges(field);

Clear form field data(changes). The method triggers ‘update’ event eventually

Parameters:

Type Name Description
String field Required. Field name to clear changes of.

clearChanges

  formService.clearChanges();

Clear all form data(changes). The method triggers ‘update’ event eventually