Defining validation rules
Typically, you define validation rules in the Database painter,
because validation rules are properties of the data itself. Once
defined in the Database painter, the rules are used by default each
time the column is placed in a DataWindow object. You can also define a
validation rule in the DataWindow Object painter that overrides the rule
defined in the Database painter.
Defining a validation rule in the Database painter
This section describes the ways you can manipulate validation
rules in the Database painter.
To create a new validation rule
-
In the Database painter, select Object>Insert>Validation
Rule from the menu bar.The Validation Rule view displays in the Properties view.
-
Assign a name to the rule, select the datatype
of the columns to which it applies, and customize the error message
(if desired).For information, see “Customizing the error message”.
-
Click the Definition tab and define the expression
for the rule.For information, see “Defining the expression”.
You can use this rule with any column of the appropriate datatype
in the database.
To modify a validation rule:
-
In the Database painter, open the Extended
Attributes view. -
In the Extended Attributes view, open the list
of validation rules. -
Position the pointer on the validation rule you
want to modify, display the pop-up menu, and select Properties. -
In the Validation Rule view, modify the validation
rule as desired.For information, see “Defining the expression” and “Customizing the error message”.
To associate a validation rule with a column in
the Database painter:
-
In the Database painter (Objects view),
position the pointer on the column, select Properties from the column’s
pop-up menu, and select the Validation tab. -
Select a validation rule from the Validation Rule
drop-down list.The column now has the selected validation rule associated
with it in the extended attribute system tables. Whenever you use
this column in a DataWindow object, it will use this validation rule unless
you override it in the DataWindow painter.
To remove a validation rule from a column in the
Database painter:
-
In the Database painter (Objects view),
position the pointer on the column, select Properties from its pop-up
menu, and select the Validation tab in the Properties view. -
Select (None) from the list in the Validation
Rule drop-down list.The validation rule is no longer associated with the column.
Defining the expression
A validation rule is a boolean expression. PowerBuilder applies
the boolean expression to an entered value. If the expression returns TRUE,
the value is accepted. Otherwise, the value is not accepted and
an ItemError event is triggered.
What expressions can contain
You can use any valid DataWindow expression in validation rules.
Validation rules can include most DataWindow expression functions. A DataWindow object that
will be used in PowerBuilder can also include user-defined
functions. DataWindow expression functions are displayed in the Functions list
and can be pasted into the definition.
For information about these functions, see the DataWindow Reference.
Use the notation @placeholder (where placeholder is
any group of characters) to indicate the current column in the rule.
When you define a validation rule in the Database painter, PowerBuilder stores
it in the extended attribute system tables with the placeholder
name. At runtime, PowerBuilder substitutes the value of the column
for placeholder.
Pasting the placeholder
The @col can be easily used as the placeholder. A
button in the Paste area is labeled with @col. You can
click the button to paste the @col into the validation
rule.
An example
For example, to make sure that both Age and Salary are
greater than zero using a single validation rule, define the validation
rule as follows:
|
1 |
@col > 0 |
Then associate the validation rule with both the Age and Salary columns.
At runtime, PowerBuilder substitutes the appropriate values for the
column data when the rule is applied.
Using match values for character columns
If you are defining the validation rule for a character column,
you can use the Match button on the Definition page of the Validation
Rule view. This button lets you define a match pattern for matching
the contents of a column to a specified text pattern (for example,
^[0-9]+$ for all numbers and
^[A-Za-z]+$ for all letters).
To specify a match pattern for character columns:
-
Click the Match button on the Definition
page of the Validation Rule view.The Match Pattern dialog box displays.
-
Enter the text pattern you want to match the column
to, or select a displayed pattern. -
(Optional) Enter a test value and click the Test
button to test the pattern. -
Click OK when you are satisfied that the pattern
is correct.
For more on the Match function
and text patterns, see the DataWindowReference.
Customizing the error message
When you define a validation rule, PowerBuilder automatically
creates the error message that displays by default when users enter
an invalid value:
|
1 |
'Item ~'' + @<i>Col</i> + '~' does not pass validation test.' |
You can edit the string expression to create a custom error
message.
Different syntax in the DataWindow Object painter
If you are working in the DataWindow Object painter, you can enter
a string expression for the message, but you do not use the @ sign
for placeholders. For example, this is the default message:
|
1 |
'Item ~'' + <i>ColumnName</i> + '~' does not pass validation test.' |
A validation rule for the Salary column in the Employee table
might have the following custom error message associated with it:
|
1 |
Please enter a salary greater than $10,000.' |
If users enter a salary less than or equal to $10,000,
the custom error message displays.
Specifying initial values
As part of defining a validation rule, you can supply an initial
value for a column.
To specify an initial value for a column in the
Database painter:
-
Select Properties from the column’s
pop-up menu and select the Validation tab. -
Specify a value in the Initial Value box.
Defining a validation rule in the DataWindow Object painter
Validation rules you assign to a column in the Database painter
are used by default when you place the column in a DataWindow object. You
can override the validation rule in the DataWindow Object painter by defining
an ad hoc rule for one specific column.
To specify a validation rule for a column in the
DataWindow painter:
-
In the DataWindow painter, select View>Column
Specifications from the menu bar.The Column Specification view displays.
-
Create or modify the validation expression. To
display the Modify Expression dialog box, display the pop-up menu
for the box in which you want to enter a Validation Expression and
select Expression. Follow the directions in “Specifying the expression”. -
(Optional) Enter a string or string expression
to customize the validation error message.For more information, see “Customizing the error message”.
-
(Optional) Enter an initial value.
Used for current column only If you create a validation rule here, it is used only for
the current column and is not saved in the extended attribute system
tables.
Specifying the expression
Since a user might have just entered a value in the column,
validation rules refer to the current data value, which you can
obtain through the GetText DataWindow expression function.
Using GetText ensures that the most recent
data entered in the current column is evaluated.
PowerBuilder does the conversion for you If you have associated a validation rule for a column in the
Database painter, PowerBuilder automatically converts the syntax to
use GetText when you place the column in a DataWindow object.
GetText returns a string. Be sure to use
a data conversion function (such as Integer or Real)
if you want to compare the entered value with a datatype other than
string.
For more on the GetText function
and text patterns, see the DataWindowReference.
Referring to other columns
You can refer to the values in other columns by specifying
their names in the validation rule. You can paste the column names
in the rule using the Columns box.
Examples
Here are some examples of validation rules.
Example 1 To check that the data entered in the current column is a
positive integer, use this validation rule:
|
1 |
Integer(GetText( )) > 0 |
Example 2 If the current column contains the discounted price and the column
named Full_Price contains the full
price, you could use the following validation rule to evaluate the
contents of the column using the Full_Price column:
|
1 |
Match(GetText( ),"^[0-9]+$") AND<br />Real(GetText( )) < Full_Price |
To pass the validation rule, the data must be all digits (must
match the text pattern ^[0-9]+$)
and must be less than the amount in the Full_Price column.
Notice that to compare the numeric value in the column with
the numeric value in the Full_Price column,
the Real function was used to convert the text
to a number.
Example 3 In your company, a product price and a sales commission are related
in the following way:
- If
the price is greater than or equal to $1000, the commission
is between 10 percent and 20 percent - If the price is less than $1000, the commission
is between 4 percent and 9 percent
The Sales table has two columns, Price and Commission.
The validation rule for the Commission column
is:
|
1 |
(Number(GetText( )) >= If(price >= 1000, .10, .04))<br />AND<br />(Number(GetText( )) <= If(price >= 1000, .20, .09)) |
A customized error message for the Commission column
is:
|
1 |
"Price is " + if(price >= 1000,<br />"greater than or equal to","less than") +<br />" 1000. Commission must be between " +<br />If(price >= 1000,".10", ".04") + " and " +<br />If(price >= 1000, ".20.", ".09.") |