Skip to main content

Problem:

This demo contains a number of Dialog System screensets togther with COBOL programs to illustrate how to apply DATA VALIDATION

Resolution:

How do I validate fields as soon as they lose focus?

The most common problem with field-by-field validation is that an application can end up in an infinite loop of validation errors. For example, if an entry field loses focus and is validated, and the validation fails, then focus is returned to the enrty field. However, this causes another control to lose focus, and if that control is also validated, then an infinite loop can result.

Another problem is that you may sometimes not want to validate a field, for example if a user clicks the Cancel or Help buttons on a dialog box, or switches to a different application.

This screenset illustrates one way to implement field-by-field validation which avoids the infinite looping problems, and allows the implementation of Cancel and Help buttons:

Define the following additional items in the data block:                                                           :

       VALIDATION-ERROR-FOUND        C5    4.0

       VALIDATION-ERROR-FIELD          C5    4.0

       VALIDATION-ERROR-MESSAGE     X    80.0

If you already have an error message field defined in your screenset, then use that field instead of defining the VALIDATION-ERROR-MESSAGE. (Otherwise define the VALIDATION-ERROR-MESSAGE field as the error field).

Add the following global dialog:

     *

     * Procedure to Report a validation error

     * PROC-REPORT-VAL-ERROR

          INVOKE-MESSAGE-BOX MBOX-ERROR ERROR-MSG-FIELD $REGISTER

          SET-FOCUS VALIDATION-ERROR-FIELD

          TIMEOUT 1 PROC-CLEAR-VAL-ERR-TIMEOUT

     *

     * Reset the validation error flag and TIMEOUT

     * PROC-CLEAR-VAL-ERR-TIMEOUT

          MOVE 0 VALIDATION-ERROR-FOUND

          TIMEOUT 0 $NULL

     *

     * Do-nothing procedure. Contains no functions.

     * PROC-DO-NOTHING

The REPORT-VALIDATION-ERROR procedure is executed after a short timeout. It reports a validation error that has been detected, then sets focus on the field that is in error. Another timeout is then set to execute the procedure PROC-CLEAR-VAL-ERR-TIMEOUT once all the focus events have been processed. This event clears the flags associated with the validation failure.

Add the following to SCREENSET-INITIALIZED dialog:

    MOVE 0 VALIDATION-ERROR-FOUND

This clears the VALIDATION-ERROR-FOUND flag ready for the first validation error.

For any objects which are to be validated on the LOST-FOCUS dialog, add the following line of dialog as the first line of LOST-FOCUS dialog:

    IF= VALIDATION-ERROR-FOUND 1 PROC-DO-NOTHING

Add the following dialog to the VAL-ERROR event in global dialog

       MOVE $EVENT-DATA VALIDATION-ERROR-FIELD

       MOVE 1 VALIDATION-ERROR-FOUND

       TIMEOUT 1 PROC-REPORT-VAL-ERROR

($EVENT-DATA is set to the object ID when a VAL-ERROR occurs.)

If you want to cancel a validation error before it is reported add the following dialog where the error is to be cancelled:

       EXECUTE-PROCEDURE PROC-CLEAR-VAL-ERR-TIMEOUT

If you wish to cancel a validation error on a push button, you   need to cancel the error on a GAINED-FOCUS event on the button, not the BUTTON-SELECTED event. A timeout can happen between the GAINED-FOCUS and BUTTON-SELECTED events, which would trigger the validation error message.

It is also particularly important to cancel any validation error when your application loses focus, otherwise you will prevent users from switching to a different application, and you will also interfere with the operation of Screenset Animator and the NetExpress IDE.

You can do this by implementing LOST-FOCUS dialog on the window. Note that this also applies when switching focus to other windows in your application.

Now, whenever a validation error causes the focus to be changed, this will set the VALIDATION-ERROR-FOUND flag, and set a very short TIMEOUT to clear the flag again. Since TIMEOUT events do not happen if there are any events still to be processed, the TIMEOUT event is guaranteed to happen after the LOST-FOCUS event. If the LOST-FOCUS event finds the VALIDATION-ERROR-FOUND flag set, then it doesn't validate the field that lost focus (and so doesn't cause a focus change).

==========================================================

Keywords: demonstration, sample, example, demo, Dialog System, validation.zip

demo.ex

demo.ne

Attachments:

validation.zip

Old KB# 4240