Hi guys, I've encountered the following situation:
I have to modify a datasource's validateWrite method on an extended form, to which I have to add some logic before and after it executes the parent validation that modifies the return value on the validateWrite method.
How can I approach that using event handlers (if that's possible)? More specifically, how can I modify its return value and add code before and after the super is called?
I'm currently developing on Update 7.
Thanks!
See this link : http://dax365.blogspot.com/2017/06/ax-7-validating-events-on-form-data.html
You can cancel the return event :
[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); } } } }
I have the same question, any answer for this yet?
Thanks,
SWetha K Desai
Ok, but how do I tell the "parent" validateWrite that the validation on the event handler was successful or not without passing it the returned value?
By looking at your requirement , I don't feel you have to return any value , you can just perform your validation in Pre or post event handler and if validation is ok use that value further.
Well, for example, I have extended the SalesEditLines form and added a datasource to it. On this new datasource I have to override the validateWrite, because I need to use a variable on the form level (documentStatus) to check if it's an invoice or a packing slip and perform a different validation based on that value... so it's crucial that the validateWrite is on the datasource and not on the table in this particular case.
That's one of the many cases I'm facing, each of them different.
Do you think it is achievable to modify the validateWrite on the datasource in the same way as the example you provided?
I am not sure about your requirement, Is there any reason you are not writing this on table EventHandler and change value there and return it. May be you can specify your form name and what you want to achieve so that we get clear Requirement.
Thank you for the help Sukrut. Sorry for the delay, I didn't read the notification of your message until today.
The example you provided is for the validateWrite on the table, am I right? How can I make it work for the validateWrite on the datasource of a form? Because the event handler has different parameters, and none of those objects has the parmValidateResult method to modify the original return value:
/// <summary>
/// Event handler for datasource validateWrite
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
[FormDataSourceEventHandler(formDataSourceStr(LedgerJournalTable, LedgerJournalTable), FormDataSourceEventType::ValidatedWrite)]
public static void LedgerJournalTable_OnValidatedWrite(FormDataSource sender, FormDataSourceEventArgs e)
{
}
Again, thank you.
You just have to copy those events and implement in your eventHandler class. Here is example you can return value through DateEventArgs variable
[DataEventHandler(tableStr(AnyTable), DataEventType::ValidatedWrite)]
public static void InventLocation_onValidatedWrite(Common sender, DataEventArgs e)
{
// convert Common to AnyTable
AnyTable anyTable = sender;
// the DataEventArgs actually are ValidateEventArgs and can be converted
ValidateEventArgs validateEventArgs = e;
// the ValidateEventArgs carry the validation result (so far)
boolean ret = validateEventArgs.parmValidateResult();
// the table has some additional validation logic and gives back the result
ret = anyTable.doSomeAdditionalCustomValidation(ret);
// provide the args with the validation result
validateEventArgs.parmValidateResult(ret);
}
Hi Sukrut, thank you for answering. How could I modify the returned value in that case? I've been using the approach on the link you provided, but only for fields on the datasource... and that brings me to the next question; how could I override a datasource method by code?
To execute logic before super you can write your logic on OnValidatingWrite and after super you can write on OnValidatedWrite event for the form datasource. You can see examples in this link