User input validation with Smart Office scripts

Here are several solutions to validate user input with Personalized Scripts in Lawson Smart Office, from the simple click of a button, to using a keyboard timer “à la” Google Suggest.

Background

To ensure the integrity of a system it is important to validate user input before it is submitted; incorrect data could compromise the system.

M3 automatically validates most fields that have a functional significance in the Business Engine, for example a date must have a valid format MMDDYY, an Item number must exist in MMS001, or a Bank Account information in CRS692 must be associated with an existing Customer in CRS610.

But M3 does not validate everything, for example it does not validate phone numbers, nor addresses.

In this post I illustrate several techniques to validate user input in Smart Office using Personalized Scripts.

1) Manual validation with button

A simple solution to validate the user input is to dynamically add a “Validate” button on the panel, and let the user click it. This is a simple solution to implement. But it is manual, and it depends on the user self-disciplining and clicking on the button. It is not ideal for enforcing validation.

2) Automatic validation on submit

An automatic solution to enforce validation is to intercept the Smart Office request to the server when the user presses ENTER or clicks Next. For that, we listen to the Requesting event, we validate the user input, and we eventually cancel the request. There is a great example named CancelRequestExample in the Lawson Smart Office Developer’s Guide; refer to the Developer’s Guide to copy the source code:

3) Automatic validation when typing

Another automatic solution to enforce validation is to listen to the TextChanged event of the desired TextBox control:

public function Init(element: Object, args: Object, controller: Object, debug: Object) {
    element.add_TextChanged(OnTextChanged);
}
function OnTextChanged(sender: Object, e: TextChangedEventArgs) {
    if (sender.Text /*validation expression here*/) {
        // valid
    } else {
        // invalid
    }
}

But this solution will trigger the event every time the user types on the keyboard, at each keystroke. This could lead to bad usability in some cases, for example if the validation function highlights the TextBox’s background in red if the value is incorrect, the TextBox would flicker unnecessarily as the user is typing.

In the following screenshot, we see the validation expression being evaluated at each keystroke as I type “Hello World” in the TextBox:

4) Automatic validation with a Timer “à la” Google Suggest

A better solution than the above is to use a keyboard timer and validate the user input only after the user has finished typing. For that we use a DispatcherTimer and the Tick Event. That will only validate the user input when the user is not longer typing, similar to Google Suggest.

This solution is better from a usability point of view.

Also, this solution is better from a performance point of view where the validation function is time and resource consuming, for example if it needs to call an M3 API or a Lawson Web Service to validate the user input, this solution would minimize stress on the server.

In the following screenshot, we see the validation expression being evaluated only twice, once per word; I apparently have a fraction of a second pause between words:

Here is the complete source code:

import System;
import System.Windows;
import System.Windows.Controls;
import System.Windows.Threading;
import MForms;

package MForms.JScript {
    class Test {
        var element, controller, debug, timer;
        public function Init(element: Object, args: Object, controller : Object, debug : Object) {
            this.element = element;
            this.controller = controller;
            this.debug = debug;
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 0, 0, 250); // milliseconds
            timer.add_Tick(OnTick);
            element.add_TextChanged(OnTextChanged);
            controller.add_Requested(OnRequested);
        }
        /* Started typing */
        function OnTextChanged(sender: Object, e: TextChangedEventArgs) {
            timer.Stop();
            timer.Start();
        }
        /* Stopped typing */
        function OnTick(sender: Object, e: EventArgs) {
            timer.Stop();
            if (element.Text /* validation expression here */) {
                // valid
            } else {
                // invalid
            }
        }
        /* Clean-up */
        function OnRequested(sender: Object, e: RequestEventArgs) {
            controller.remove_Requested(OnRequested);
            timer.remove_TextChanged(OnTextChanged);
            timer.remove_Tick(OnTick);
            timer.Stop();
        }
    }
}

Conclusion

In this post I introduced several techniques to validate user input in Smart Office using Personalized Scripts, from a simple and manual technique, to an automatic and more advanced technique with a keyboard timer for better usability and performance.

Also, it is important to emphasize that the above solutions will only cover input validation at the user interface level, from Smart Office. They will not cover low-level user input from other entry points such as M3 API, or from Lawson Web Services of type M3 Display Program (MDP). In those cases, only an M3 Java modification with MAK would be able to validate user input.

Published by

thibaudatwork

ex- M3 Technical Consultant

7 thoughts on “User input validation with Smart Office scripts”

  1. Hello,
    very clear and interesting topic.
    Do you think there is way to an event (like in method 3) to a textbox itself included in a listview ? some of the list in M3 allows full screen entry like PMS050 or MMS424

    Like

Leave a comment