Structuring Functions for Inline Validation
Structuring Functions to Use a Validation Subroutine
Using the CONDCHECK Command for Inline Validation
Using the CONDCHECK Command for Validation with a Subroutine
Structuring Functions for Inline Validation
Typically functions using validation commands (eg: CONDCHECK, DATECHECK, FILECHECK, RANGECHECK and VALUECHECK) are structured for inline validation like this:
BEGIN_LOOP
REQUEST << INPUT >>
BEGINCHECK
* << USE CHECK COMMANDS TO VALIDATE INPUT HERE >>
ENDCHECK
* << PROCESS THE VALIDATED INPUT HERE >>
END_LOOP
If a validation command inside the BEGINCHECK / ENDCHECK command block detects a validation error control is passed back to the REQUEST command. This happens because of the default IF_ERROR(*LASTDIS) parameter on the ENDCHECK command.
Structuring Functions to Use a Validation Subroutine
Typically functions using validation commands (eg: CONDCHECK, DATECHECK, FILECHECK, RANGECHECK and VALUECHECK) are structured for subroutine validation like this:
DEFINE FIELD(#ERRORCNT) REFFLD(#STD_NUM)
DEF_COND NAME(*NOERRORS) COND('#ERRORCNT = 0')
BEGIN_LOOP
DOUNTIL COND(*NOERRORS)
REQUEST << INPUT >>
EXECUTE SUBROUTINE(VALIDATE)
ENDUNTIL
* << PROCESS THE VALIDATED INPUT HERE >>
END_LOOP
SUBROUTINE NAME(VALIDATE)
CHANGE FIELD(#ERRORCNT) TO(0)
BEGINCHECK KEEP_COUNT(#ERRORCNT)
* << USE CHECK COMMANDS TO VALIDATE INPUT HERE >>
ENDCHECK IF_ERROR(*NEXT)
ENDROUTINE
If a validation command inside the BEGINCHECK / ENDCHECK command block detects a validation error control is returned to the main function loop with #ERRORCNT > 0.
Using the CONDCHECK Command for Inline Validation
This example demonstrates how to use the CONDCEHECK command within the main program block to check the value of a field against a set of conditions. Here the salary of the new employee is added to the current salary of the department and checked that it is still under the salary budget.
DEFINE FIELD(#NEWSALARY) REFFLD(#SALARY) LABEL('New Salary')
DEFINE FIELD(#TOTSALARY) REFFLD(#SALARY) DEFAULT(0)
DEFINE FIELD(#BUDGET) REFFLD(#SALARY) LABEL('Budget')
DEF_LIST NAME(#EMPBROWSE) FIELDS(#EMPNO #NEWSALARY)
BEGIN_LOOP
REQUEST FIELDS(#DEPTMENT #BUDGET #EMPNO #NEWSALARY) BROWSELIST(#EMPBROWSE)
CHANGE FIELD(#TOTSALARY) TO(*DEFAULT)
SELECT FIELDS(#SALARY) FROM_FILE(PSLMST1) WITH_KEY(#DEPTMENT)
CHANGE FIELD(#TOTSALARY) TO('#TOTSALARY + #SALARY')
ENDSELECT
BEGINCHECK
CONDCHECK FIELD(#NEWSALARY) COND('(#NEWSALARY + #TOTSALARY) <= #BUDGET') MSGTXT('New salary causes Department budget to be exceeded')
ENDCHECK
ADD_ENTRY TO_LIST(#EMPBROWSE)
END_LOOP
If the salary for the new employee, when added to all existing salaries for that department, exceeds the budget for salaries the message defined with the CONDCHECK command is issued and program control returns to the last screen displayed. In this case the last screen displayed is the REQUEST screen.
Using the CONDCHECK Command for Validation with a Subroutine
This example demonstrates how to use the CONDCHECK command inside a subroutine to check the value of a field against a set of conditions.
After the user enters the requested details the VALIDATE subroutine is called. It checks that the salary of the new employee when added to all existing salaries for the department is still under the salary budget. If this condition is not true the message defined in the CONDCHECK command is given and the DOUNTIL loop executes again. When this condition is true the DOUNTIL loop ends and processing of the verified input is done.
DEFINE FIELD(#ERRORCNT) TYPE(*DEC) LENGTH(3) DECIMALS(0) DEFAULT(0)
DEF_COND NAME(*NOERRORS) COND('#ERRORCNT = 0')
DEFINE FIELD(#NEWSALARY) REFFLD(#SALARY) LABEL('New Salary')
DEFINE FIELD(#TOTSALARY) REFFLD(#SALARY) DEFAULT(0)
DEFINE FIELD(#BUDGET) REFFLD(#SALARY) LABEL('Budget')
DEF_LIST NAME(#EMPBROWSE) FIELDS(#EMPNO #NEWSALARY)
BEGIN_LOOP
DOUNTIL COND(*NOERRORS)
REQUEST FIELDS(#DEPTMENT #BUDGET #EMPNO #NEWSALARY) BROWSELIST(#EMPBROWSE)
EXECUTE SUBROUTINE(VALIDATE)
ENDUNTIL
ADD_ENTRY TO_LIST(#EMPBROWSE)
END_LOOP
SUBROUTINE NAME(VALIDATE)
CHANGE FIELD(#ERRORCNT) TO(0)
CHANGE FIELD(#TOTSALARY) TO(*DEFAULT)
SELECT FIELDS(#SALARY) FROM_FILE(PSLMST1) WITH_KEY(#DEPTMENT)
CHANGE FIELD(#TOTSALARY) TO('#TOTSALARY + #SALARY')
ENDSELECT
BEGINCHECK KEEP_COUNT(#ERRORCNT)
CONDCHECK FIELD(#NEWSALARY) COND('(#NEWSALARY + #TOTSALARY) <= #BUDGET') MSGTXT('New salary causes Department budget to be exceeded')
ENDCHECK IF_ERROR(*NEXT)
ENDROUTINE