Step 4 Add Program Level Validations

Visual LANSA

Step 4. Add Program Level Validations

FRM035 - Maintain a Simple Database Table

In this step you will learn how to create program level validations using code within a BEGINCHECK/ENDCHECK block.

Note that this exercise is just an example created to explain validation commands. Most of your validation rules will be placed in the Repository as part of the file definitions. You may also design your forms to reduce the number and types of program validations required. For instance, a drop down list box provides a list of values so that VALUECHECK is not required.

1.  Add a validation check that ensures the Department Code does not contain any embedded blanks.

     For example, 'A A' should not be allowed as a valid code. Use a CONDCHECK statement within a BEGINCHECK / ENDCHECK block and the CONTAINS intrinsic field method to search for blank characters.

     Add the following code immediately before the INSERT command in the INSERT.Click event routine:

BEGINCHECK
CONDCHECK FIELD(#DEPTMENT) COND(#DEPTMENT.Contains( ' ' )) IF_TRUE(*ERROR)
IF_FALSE(*NEXT) MSGTXT('Code cannot contain embedded blanks.')
ENDCHECK

 

     Note: CONDCHECK. . . . . IF_FALSE( . . . . . is a single command, on a single line. It is shown here on two lines due to space limitations. You should type CONDCHECK and then use the Command Assistant (F4) to complete this code.

2.  Compile and execute the form.

a.  Enter a Department Code that includes a blank space and press the Insert button.

     Notice the field in error and the error message.

     The repository validation rules (checking that Department Description is not blank) have not been invoked because the INSERT command has not yet been executed because of the error detected in BEGINCHECK/ENDCHECK.

3.  Close the form.

4.  Another option to using the CONDCHECK is to use a simple IF statement combined with the SET_ERROR command. Using SET_ERROR allows more than one field to be set in error.

a.  Add a rule that checks that the user has not entered the same values for the DEPTMENT and DEPTDESC fields. If they are the same, set both fields in error.

BEGINCHECK
CONDCHECK FIELD(#DEPTMENT) COND(#DEPTMENT.Contains( ' ' )) IF_TRUE(*ERROR)
IF_FALSE(*NEXT) MSGTXT('Code cannot contain embedded blanks.')

IF COND(#DEPTMENT *EQ #DEPTDESC)
SET_ERROR FOR_FIELD(#DEPTMENT #DEPTDESC) MSGTXT('Department Code cannot be the same
as Department Description.')
ENDIF

ENDCHECK

 

5.  Add a RANGECHECK validation to check if STD_NUM is between 1 and 10.

     Remember this field is not used in the DEPTAB file. You are simply using program level validations to check the values of the fields on the screen.

RANGECHECK FIELD(#STD_NUM) RANGE((1 10)) MSGTXT('Must be in range 1 to 10.')
 

6.  Add a VALUECHECK validation to check if the DEPTDESC field is in a list of reserved values NONE, END or LAST.

VALUECHECK FIELD(#DEPTDESC) WITH_LIST(NONE END LAST) IN_LIST(*ERROR)

NOT_INLIST(*NEXT) MSGTXT('This description is reserved.')

7.  Finally, check that the Department Code does not already exist in the file DEPTAB. Note that this check is not necessary as duplicate key fields are automatically checked based on the file attributes. This is just an example. Typically, you might check that the field is present in a different file.

FILECHECK FIELD(#DEPTMENT) USING_FILE(DEPTAB) USING_KEY(#DEPTMENT) FOUND(*ERROR)

NOT_FOUND(*NEXT) MSGTXT('Department Code already exists.')

 

     Your finished validation code should appear as follows:

BEGINCHECK
FILECHECK FIELD(#DEPTMENT) USING_FILE(DEPTAB) USING_KEY(#DEPTMENT) FOUND(*ERROR)
NOT_FOUND(*NEXT) MSGTXT('Department Code already exists.')
VALUECHECK FIELD(#DEPTDESC) WITH_LIST(NONE END LAST) IN_LIST(*ERROR)
NOT_INLIST(*NEXT) MSGTXT('This description is reserved.')
RANGECHECK FIELD(#STD_NUM) RANGE((1 10)) MSGTXT('Must be in range 1 to 10.')
CONDCHECK FIELD(#DEPTMENT) COND(#DEPTMENT.Contains( ' ' )) IF_TRUE(*ERROR)
IF_FALSE(*NEXT) MSGTXT('Code cannot contain embedded blanks.')
IF COND(#DEPTMENT *EQ #DEPTDESC)
SET_ERROR FOR_FIELD(#DEPTMENT #DEPTDESC) MSGTXT('Department Code cannot be the same as Department Description.')
ENDIF
ENDCHECK

 

8.  Compile and execute the form.

a.  Enter a Department Code of ADM and a Description of XYZ. Leave the STD_NUM as 0 and press the Insert button.

b.  Notice the fields in error and scroll through the error messages displayed.

c.  Try entering identical values for the Department Code and Description.

9.  Close the form.

     It is recommended that you review information about other validation commands. Using the LANSA Technical Reference, you should review the CALLCHECK and DATECHECK commands.