Oracle® Application Development Framework Developer's Guide For Forms/4GL Developers 10g (10.1.3.1.0) Part Number B25947-01 |
|
|
View PDF |
One page of the Entity Object Editor worthy of special attention is the Validation page, where you can see and manage the declarative validation rules for the entity or any of its attributes. The framework enforces entity-level validation rules when a user tries to commit pending changes or simply navigates between rows. Attribute-level validation rules are enforced when the user changes the value of the related attribute. When you add a validation rule, you supply an appropriate error message and can later translate it easily into other languages if needed. Oracle ADF ships with a number of built-in declarative validation rules that you'll see in this section. Section 9.3, "Using Method Validators" explains how to use the Method Validator to invoke custom validation code and in Section 26.9, "Implementing Custom Validation Rules" you'll learn how to extend the basic set of declarative rules with custom rules of your own.
To add a validation rule to an entity object, use the Validation page of the Entity Object Editor, as shown in Figure 6-17. To add an attribute-level validation rule, select the attribute in the Declared Validation Rules tree, and click New.... Defining an entity-level validation rule is similar, except that you select the root entity object node in the tree before clicking New....
When you add a new validation rule, the Add Validation Rule dialog appears. Use the Rule dropdown list to select the kind of validation rule you want, and configure its declarative settings using the other controls in the page. The controls will change depending on the kind of validation rule you select. Figure 6-18 illustrates what the Add Validation Rule dialog would look like when defining a range validation rule for the ProdId
attribute of the ServiceRequest
entity object. This validation rule has been selected to enforce that the value lie between 100
and 999
inclusive. When you add a validation rule, you also can enter an error message that will be shown to the user if the validation rule fails.
When you add a validation rule to an entity object, JDeveloper updates its XML component definition to include an entry describing what rule you've used and what rule properties you've entered. For example, if you add the range validation rule above to the ProdId
attribute, this results in a RangeValidationBean
entry in the XML file:
<Entity Name="ServiceRequest" <!-- : --> <Attribute Name="ProdId" IsNotNull="true" Precision="8" Scale="0" ColumnName="PROD_ID" Type="oracle.jbo.domain.Number" ColumnType="NUMBER" SQLType="NUMERIC" TableName="SERVICE_REQUESTS" > <RangeValidationBean xmlns="http://xmlns.oracle.com/adfm/validation" ResId="ProdId_Rule_0" OnAttribute="ProdId" OperandType="LITERAL" MinValue="100" MaxValue="999" > </RangeValidationBean> </Attribute> <!-- : --> </Entity>
At runtime, the rule is automatically enforced by the entity object based on this declarative information. The error message is a translatable string and is managed in the same way as translatable UI control hints in an entity object message bundle class. The ResId
property in the XML component definition entry for the validator corresponds to the String
key in the message bundle. Example 6-4 shows the relevant bit of the ServiceRequest
entity object's message bundle, where the ProdId_Rule_0
key appears with the error message for the default locale. The validation errors messages get translated using the same mechanism described above for UI control hints.
Example 6-4 Entity Object Message Bundle Contains Validation Error Messages
package devguide.model.entities.common; import oracle.jbo.common.JboResourceBundle; // --------------------------------------------------------------------- // --- File generated by Oracle ADF Business Components Design Time. // --------------------------------------------------------------------- public class ServiceRequestImplMsgBundle extends JboResourceBundle { static final Object[][] sMessageStrings = { // other strings here { "ProdId_Rule_0", "Valid product codes are between 100 and 999" }, // other strings here }; // etc. }
{para}?>It is important to know that some validators can be used at the entity level, and some are used on the attribute level. Also, you should be aware the List Validator is designed for working with a relatively small set.
You can use the following built-in validators at the entity object level:
Validates that the primary key for an entity is unique.
Invokes a method in an entity object's custom Java class to evaluate a programmatic validation.
You can use the following built-in validators at the entity object attribute level:
Validates an attribute value against:
A literal value,
A selected attribute of the first row of a view object query result, or
The first column of the first row of a SQL query result
Validates that an attribute exists in an in-memory set of values from a:
Static list,
A selected attribute in the rows of view object's default row set, or
The first column value in the rows of a SQL query result.
Validates that an attribute lies between a minimum and maximum value, inclusive.
Validates whether the string length of an attribute's value is less than, equal to, or greater than a fixed number of characters.
Validates that an attribute's value matches a regular expression.
Invokes a method in an entity object's custom Java class to evaluate a programmatic validation.
The List Validator is designed for validating an attribute against a relatively small set of values. As shown in Figure 6-19, if you select the Query Result or View Object Attribute style of list validation, keep in mind the validator will retrieve all of the rows from the query before performing an in-memory scan to validate whether the attribute value in question matches an attribute in the list. The query performed by the Validator's SQL or view object query does not reference the value being validated in the WHERE
clause of the query.
In other words, this is not the feature to use if you want to validate that a user-entered product code exists in a table of a million products. Section 9.6, "Using View Objects for Validation", explains the technique you can use to efficiently perform SQL-based validations by using a view object to perform a targeted validation query against the database.