Tuesday, 20 June 2017

AX 7 : Validating events on form data sources and form data source fields

Validation events on form data source (FormDataSourceEventType) and form data source fields (FormDataFieldEventType) now support invalidating user-specified values. 
The following example illustrates this feature. The example uses a form named MyForm that contains a data source named abTable, and a field named FieldInt1.

public class abFormEvent
{
/// <summary> /// Disallows inserting records on the form data source if the Field1 field contains the integer value 1 /// </summary> [FormDataSourceEventHandler(formDataSourceStr(MyForm, abTable), FormDataSourceEventType::ValidatingWrite)] public static void abTable_OnValidatingWrite(FormDataSource sender, FormDataSourceEventArgs e) { var datasource = sender as FormDataSource; var args = e as FormDataSourceCancelEventArgs; if (args != null && datasource != null) { var record = datasource.cursor() as abTable; if (record.recId == 0) { if (record.FieldInt1 == 1) { boolean doCancel = !checkFailed("Value 1 is not allowed"); args.cancel(doCancel); } } } } /// <summary> /// Disallow changing the Field1 value on the form data source field,if the Field1 value contains 10 /// </summary> [FormDataFieldEventHandler(formDataFieldStr(MyForm,abTable,FieldInt1),FormDataFieldEventType::Validating)] public static void FieldInt1_OnValidating(FormDataObject sender,FormDataFieldEventArgs e) { var dataObject = sender as FormDataObject; var args = e as FormDataFieldCancelEventArgs; if (args != null && dataObject != null) { var datasource = dataObject.datasource() as FormDataSource; if (datasource != null) { var record = datasource.cursor() as abTable; if (record.RecId > 0) { if (record.FieldInt1 == 10) { boolean doCancel =!checkFailed("Value 10 not allowed"); args.cancel(doCancel); } } } } } }

1 comment:

  1. do u have validation code for form level like form Onclosing .?

    ReplyDelete