Full validation on custom event with form engine

By default, there are the default events that before they are fired, the validation function must be called or can be called optionally to validate data based on the rules provided in the model file. However, if you create a custom event and you do want to validate the data on the form before firing that event, A12 provides an extension point for you to hook that custom event in form-engine and more important, it allows you to perform a validation action by calling a validation API. Let’s see how can we do that in this post.

First of all, you need to create a custom event and link to a button that you want to perform a full validation. For instance, I would like to create an export button in the form model to export current form data to a JSON file and name the event is event_export_json.

After that having that custom event in your model, you need to write custom code to handle for that event in order to handle exporting data as JSON. In that custom code, you can call to validationFully function available in FormEngine to validate data in the current form and decide what to do next with the result returned by that function.

Below is an example of using FormStateController to handle a custom event and perform the validation function.

import { FormStateController } from "@com.mgmtp.a12/client-frame-angularjs/lib/master-detail-layout/lib/ui-router-state-controller/FormState.controller";
export class CustomFormStateController extends FormStateController {
	...
	protected wireRowActionEvents(): void {
		this.eventButtonHandlers.when(/event_export_json/, this.handleExportJSONButton);
		super.wireRowActionEvents();
	}
	...
	protected handleExportJSONButton(controller: CustomFormEngineController, eventName: string): void {
		...
		controller.API.then(formEngine => {
			// call validation
			const documentValidateResult = formEngine.validateFully();
			// your custom code to use the result
			...
		}
	}
	...
}

You can use this validation function for many purposes depends on the requirement you have. Above is just a simple example of how to validate data in a custom event.

geat to see this docu.Thanks!
Question (might go to @baschir-loud-bluff, too ?) : do we get the validation results shown by the form engine without any further configuration here? The question applies certainly just to version 23.1 and above.

You can trigger the validation - independently of a configuration of a button (is that what you mean) by doing:

No, I just meant whether the results of validations are shown and are possible to be navigated to, in this case, too, or just if the engine calls the validation (triggred by submit)

Are you mention regarding correction mode? When errors are displayed to specific field you can navigate to it inside correction mode.

yes i was just wandering whether this works even for custom calls from our own events. So it does. thanks,

engine.validateFully() does three things

  • conduct a full validation
  • if errors are present, switch to correction mode
  • return the validation result

We will fix the documentation, which currently is missing the second point.