Step 4. Add Program Level Validations

In this step you will learn how to create program level validations using code within a BEGINCHECK/ENDCHECK block. All program level validation code must be within a Begincheck/Endcheck block.

Note: This exercise is just an example designed to explain validation commands. Most of your validation rules will be placed in the Repository as part of the field and table definitions. You may also design your forms to reduce the need for program validations. For instance, a drop down list box provides a list of valid values so that VALUECHECK is not required.

1.  Add a validation check that ensures the Department Code does not contain any embedded blanks. The validation code must be at the beginning of the Insert.Click event routine.

     For example, '1 9' 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:

Evtroutine Handling(#INSERT.Click)

Begincheck

Condcheck Field(#xDepartmentCode) Cond(#xDepartmentCode.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 may be displayed as two lines due to your screen size. You could 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 validation rules defined for the fields and table (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.

     Note: The Endcheck statement has an If_Error parameter that defaults to *LASTDIS. Just like error handling for I/O commands, in a component, this means branch to the Endroutine on error.

3.  Close the form.

4.  Instead of using the CONDCHECK, a simple IF statement combined with the SET_ERROR command could have been used. 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 xDepartmentCode and xDepartmentDescription fields. If they are the same, set both fields in error.

Begincheck

Condcheck Field(#xDepartmentCode) Cond(#xDepartmentCode.Contains( ' ' )) If_True(*ERROR) If_False(*NEXT) Msgtxt('Code cannot contain embedded blanks.')

If Cond(#xDepartmentCode = #xDepartmentDescription)

Set_Error For_Field(#xDepartmentCode #xDepartmentDescription) 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 xDepartments table. 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 xDepartmentDescription field is in a list of reserved values NONE, END or LAST.

#xDepartmentDescription := #xDepartmentDescription.UpperCase

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

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

     Note: Converting the input value to uppercase will ensure the listed values are not used in any form.

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

FILECHECK FIELD(#xDepartmentCode) USING_FILE(xDepartments) USING_KEY(#xDepartmentCode) 

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

 

     Your finished validation code should appear as follows:

Begincheck

Condcheck Field(#xDepartmentCode) Cond(#xDepartmentCode.Contains( ' ' )) If_True(*ERROR) If_False(*NEXT) Msgtxt('Code cannot contain embedded blanks.')

If Cond(#xDepartmentCode = #xDepartmentDescription)

Set_Error For_Field(#xDepartmentCode #xDepartmentDescription) Msgtxt('Department Code cannot be the same as Department Description')

Endif

Rangecheck Field(#STD_NUM) Range((1 10)) Msgtxt('Must be in the range 1 to 10')

#xDepartmentDescription := #xDepartmentDescription.UpperCase

Valuecheck Field(#xDepartmentDescription) With_List(NONE END LAST) In_List(*error) Not_Inlist(*next) Msgtxt('This descriptiuon is reserved')

Filecheck Field(#xDepartmentCode) Using_File(xDepartments) Using_Key(#xDepartmentCode) Found(*ERROR) Not_Found(*NEXT) Msgtxt('Department Code already exists')

Endcheck

 

8.  Compile and execute the form.

a.  Enter a Department Code of 1000 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, see also the CALLCHECK and DATECHECK commands.