Adding Computations, Processes, and Validations

Validations, computations, and processes greatly increase the functionality of an HTML DB application. In this three-part series (of which this is the first part), you’ll learn about the different types and where to use them. This article is excerpted from chapter 13 of the Oracle HTML DB Handbook, written by Lawrence Linnemeyer and Bradley Brown (McGraw-Hill, 2006; ISBN: 0072257687).

Validations, computations, and processes are the mechanism by which the developer implements business logic. We have seen processes that have been created for us by the multitude of wizards within HTML DB—some to populate fields, some to process changes to a record, others to process multiple records.

Recall from Chapter 8 the method by which a page is rendered and then processed after it is submitted. Two main HTML DB engine processes are used for these two tasks: Show Page (rendering) and Accept Page (processing). Computations and processes can be used both in the rendering of a page and in the processing of a page. Validations are only used after a page has been submitted as a means to validate the data the user has submitted for processing.

In this chapter, we will look at validations, computations, and processes and how they greatly increase the functionality of an HTML DB application. We will explore the different types and when and where to use them. These three things allow you to bring together everything else we have covered to this point.

Validations

Validations are the method HTML DB provides to the developer to ensure the quality of data entered by a user prior to the data being submitted for processing. This is an important point to remember: if any part of your validation fails, no further processing will occur. This is not to say that the remainder of the validation will not occur, just that the processing will not occur.

Validations can be either item validations or page validations. Item validations are associated with a specific item on the page and check something specific about that item’s value, such as if the item is null or if the item is greater than a certain value. Page validations are not associated with a specific item and may check a business rule that concerns several items on the page.

When a validation fails, the user is either returned to the page that was submitted or sent to an error page. One very good reason not to use the error page is that as soon as the validation process hits one failed validation, the user will navigate to the error page and see that single error message. Therefore, if they have several problems, they will make several trips to the error page. If you choose to display your error messages back on the submitted page, all the validations will occur and the user can see all the errors they must correct at the same time. The validation error messages, which are displayed back on the submitted page, can either be displayed in the submitted page’s #NOTIFICATION_MESSAGE# section or presented next to the items with which they are associated, or both.

{mospagebreak title=Creating Validations}

Validations are created with a wizard, which is started by clicking on the Add icon in the Validations section on a page’s Page Definition page. The wizard can be also be started while running a page and clicking on the Create link on the Developer’s Toolbar and selecting to create a page control on this page and then choosing Validation. At the time this book was written, this second method of starting the wizard actually dropped you into the third step of the wizard, as shown here. Therefore, to specify the level of the validation and to select an item, you must use the Previous button to navigate back to the first steps in the wizard.

In the first step of the wizard, you choose the validation level, whether the validation is for an item or for the page. If you choose item-level validation, the next step allows you to specify on which item the validation will be created. Then you move on to the third step, where you select the method of the validation you wish to create.

As you can see, there are five major categories of validation methods. The SQL, PL/SQL, and Item String Comparison methods have further choices, where you specify a type within each method (see the following table for a further explanation of SQL and PL/SQL method types). The Item String Comparison types are fairly self-explanatory, so they will not be further explained.

SQL Validation Method

Method Type

Example

Exists

select 1 from a_table where check_column = :p9_an_item

Not Exists

select 1 from a_table where check_column = :p9_an_item

SQL Expression

instr(:p10_email, ‘@’)>0

PL/SQL Validation Method

Method Type

Example

PL/SQL Expression

to_char(:p9_start_date,’MON’) != ‘JAN’

PL/SQL Error

if to_char(:p9_start_date,’MON’) != ‘JAN’ then
  raise_application_error(-20001, ‘Start Date must
  be in January’);
end if;

Function Returning Boolean

Note: The validation fails when false is returned.
if to_char(:p9_start_date,’MON’) = ‘JAN’ then
  return false;
else
 
  return true;
end if;

Function Returning Error Text

Note: return null for the validation to pass.
if to_char(:p9_start_date,’MON’) = ‘JAN’ then
  return null;
else

  return ‘The Start date must be in January’ ;
end if;

The next step in the wizard allows you to specify the sequence for the validation, which determines the order in which the validation you are creating will be evaluated among all validations. You must also provide a name. Try to make the validation names descriptive (for example, P8_LASTNAME_NOTNULL). This step is also where you specify the location in which the error message will be displayed. The choices are On an Error Page, Inline with Field, Inline with Notification, and Inline with Field and in Notification. Again, I would not recommend using the error page. Whether you want to display the error message inline with its associated field will depend on your page layout. If you have multiple items on a line, sometimes this method’s results are not very appealing.

The next step is where you actually provide the implementation of the validation. It will be a little different depending on the method you selected to create. The following example shows the implementation of a Function Returning Error Text type of PL/SQL validation method.

For some methods, you will supply an item and an expression; for others, you provide code for the validation. For all types except Function Returning Error Text, you will also have to supply the text of the error message to be displayed. As you can see in the example, you can use HTML tags to spice up the display of your messages.

The final step in creating a validation is to specify the conditions under which this validation is to be evaluated. If you do not specify any conditions, the validation will be evaluated whenever the page is submitted. You can specify the validation to be evaluated when a certain button is clicked, when a certain condition exists, or both. Using both comes in real handy when implementing a multistep wizard. You would only want to evaluate your validations when the user clicks on the Next button, and you might also want to add another condition, such as to evaluate a field only if another field is a certain value.

Editing Validations

Validations can be individually edited by clicking on the name of the validation on the Page Definition page. This opens the Edit Page Validation page, where you can edit the information collected by the wizard when creating the validation. Additionally, the normal sections for Authentication, Configuration, and Comments are available.

Clicking on the Validations title of the Validation section on the Page Definition page brings up a page where you can edit the sequence, name, and display location for all your validations at the same time. You can also see the validation type for each validation as well as if the validation is conditional and when it was last updated.

{mospagebreak title=Computations} 

Computations are bits of logic used to populate the value of an application-level item or a page item. Computations can be defined either at the application level through the shared components of an application or on an individual page. Computations defined at the application level will be executed for every page, but as with almost everything in HTML DB, you can specify a condition that will be evaluated before the computation is executed on each page.

When creating a computation, you select the computation point and the computation type. The available computation points are as follows:

  1. On New Instance (for example, On Login)
  2. Before Header
  3. After Header
  4. Before Regions
  5. After Regions
  6. Before Footer
  7. After Footer
  8. After Submit

Select the computation point that makes the most sense for the implementation of your business logic, the relationship of the item whose value you are calculating, and the other items that you might be using in the calculation. With the exception of the After Submit computation point, all the other points occur in the rendering of the page. The After Submit computation point is used for setting the value of items based on user input received from the page but prior to the execution of the page processes.

{mospagebreak title=Creating Computations}

Computations are created in two places. Application-level computations are created from the Application Computations link in the Logic section of the application’s shared components. When creating an application-level computation, you are taken directly to the Edit Application Computation page. Page-level computations are created from the Add icon in either the Computation section at the bottom of the Page Rendering column or the Computation section at the top of the Page Processing column. Both of these will start the computation-creation wizard. This wizard can also be started through the Create link on the Developer’s Toolbar and selecting the Page Control on This Page option.

The first step in the process is to select whether you want to calculate the value for an item on the same page, an item on another page, or an application-level item. Next, you select the item on which you want to create the computation from a pick list based on your first selection. You also specify the sequence for the computation. If there are multiple computations at the same processing point, they will be processed in the order of the sequence number. You also specify the computation point and the type of computation.

The six different types of computations offer the developer great flexibility in how the value of an item may be determined. Each type, along with an example, is shown in the following table.

Computation Type

Example

Static Assignment

5 or completed

PL/SQL Function Body

This is a simple example but it can be as complex as any function you would otherwise write including a declaration section.

if instr(upper(:p5_lastname),’DAN’)>0 then 
  return :p5_notes||’ it”s a Dan';
else

  return :p5_notes;
end if;

SQL Query

select code_value
  from code_table
where code = :p45_user_value;
 

SQL Expression

substr(:p5_name, instr(:p5_name, ‘,’) + 1 )

PL/SQL Expression

nvl(:p5_country, ‘Unknown’)

Item Value

p5_other_field

The last step in the wizard allows you to specify a condition that must be met before the computation will be executed. All the normal 60-plus condition types are available.

Editing Computations

To edit a page-level computation, click on the name of the computation to navigate to the Edit Page Computation page. In addition to the attributes entered in the wizard, you can specify an error message in case the computation fails. Additionally, the normal sections for Authentication, Configuration, and Comments are available.

When you click on the title of the Computations section, you will navigate to the Page Computations page, where you can edit the sequence and computation point of all your computations. You can also see the computation type, whether it is conditional, and when it was last updated.

To edit application-level computations, navigate to the specific computation through the Application Computations link in the Logic section of the application’s shared components.

Please check back next week for the continuation of this article.

[gp-comments width="770" linklove="off" ]

chat