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.
