Anda di halaman 1dari 39

Validating User Input

Copyright 2008, Oracle. All rights reserved.


Objectives

After completing this lesson, you should be able to do the


following:
Describe the types of validation available to ADF BC
applications
Decide which validation options are appropriate for
different validation requirements
Use the declarative validation options provided in
JDeveloper
Create programmatic validation
Use domains in validation

9-2 Copyright 2008, Oracle. All rights reserved.


Validation Options for ADF BC Applications

Validation options:
Business services:
ADF BC
Metadata (declarative validation)
Java (programmatic validation)
Database (PL/SQL)
User interface

9-3 Copyright 2008, Oracle. All rights reserved.


Triggering Validation Execution
The Validation Execution tab
enables you to specify:
When validation should Entity
validation rule
occur
Conditions governing
validation execution
Which attributes trigger
validation
Attribute
validation rule

9-4 Copyright 2008, Oracle. All rights reserved.


Handling Validation Errors

Use the Failure Handling tab on the Validation Rule


page of the entity object editor or attribute editor to:
Specify message
severity
Insert Groovy
variables into error
messages.
Click Select Text
Resource to store
error messages as
translatable strings

9-5 Copyright 2008, Oracle. All rights reserved.


Specifying the Severity of an Error Message

There are two severity


levels for error messages:
Error
Informational warning

9-6 Copyright 2008, Oracle. All rights reserved.


Using Groovy Variables in Error Messages

In error messages:
You indicate a variable
by surrounding a name
with braces {}
Message tokens are
added automatically
You provide message
token expressions:
source.hints.
<attr>.label for
labels
<attr> for values

9-7 Copyright 2008, Oracle. All rights reserved.


Storing Error Messages as Translatable Strings

9-8 Copyright 2008, Oracle. All rights reserved.


Defining Validation in the Business Services

Implementing validation in entity objects ensures that


validation is:
Consistent
Easily maintained
You can define validation by:
Using declarative validation:
Built-in validation rules
Custom validation rules
Using programmatic validation
Using domains: Prevalidated custom data types

9-9 Copyright 2008, Oracle. All rights reserved.


Using Declarative Validation: Built-in Rules

Built-in rules greatly reduce the need for coding.


Include the following types of rules:
Defined only at entity level:
Collection validator
Unique key validator
Can be defined at entity or attribute level, but pertain to an EO
attribute:
Compare validator
Key exists validator
Length validator
List validator
Range validator
Regular expression validator
Can be defined at entity or attribute level to validate the EO or the
attribute:
Method validator
Script Expression validator

9 - 10 Copyright 2008, Oracle. All rights reserved.


Defining Declarative Validation

Defining an
attribute-level
List validator

9 - 11 Copyright 2008, Oracle. All rights reserved.


Using Declarative Validation: Built-in Rules

Built-in rules greatly reduce the need for coding.


Include the following types of rules:
Defined only at entity level:
Collection validator
Unique key validator
Can be defined at entity or attribute level, but pertain to an EO
attribute:
Compare validator
Key exists validator
Length validator
List validator
Range validator
Regular expression validator
Can be defined at entity or attribute level to validate the EO or the
attribute:
Method validator
Script Expression validator

9 - 12 Copyright 2008, Oracle. All rights reserved.


Using Declarative Built-in Rules:
Collection Validator

Collection validators are defined at the entity level.


To define a Collection
validator, specify:
Operation
Accessor
Attribute
Operator
Compare with
Compare with value

9 - 13 Copyright 2008, Oracle. All rights reserved.


Using Declarative Built-in Rules:
Unique Key Validator

The Unique Key validator:


Is defined at entity level
Ensures that primary key values
are always unique
Can also be used for alternate
keys
Failure throws
TooManyObjectsException

9 - 14 Copyright 2008, Oracle. All rights reserved.


Using Declarative Validation: Built-in Rules

Built-in rules greatly reduce the need for coding.


Include the following types of rules:
Defined only at entity level:
Collection validator
Unique key validator
Can be defined at entity or attribute level, but pertain to an EO
attribute:
Compare validator
Key exists validator
Length validator
List validator
Range validator
Regular expression validator
Can be defined at entity or attribute level to validate the EO or the
attribute:
Method validator
Script Expression validator

9 - 15 Copyright 2008, Oracle. All rights reserved.


Using Declarative Built-in Rules:
Compare Validator
For a Compare validator, you specify: Attribute-level
Compare validator
1. Attribute (if
entity level)

2. Operator

Entity-level
Compare validator
3. Value to
compare
with

9 - 16 Copyright 2008, Oracle. All rights reserved.


Using Declarative Built-in Rules:
Key Exists Validator

The Key Exists validator:


Checks if key value exists
Checks cache first
1
2

Entity Object target type


(for all views based on this EO)
View Accessor target type
(for single view)

9 - 17 Copyright 2008, Oracle. All rights reserved.


Using Declarative Built-in Rules:
Length Validator

For a length validator, you specify:


Attribute (if defining at EO level)
Operator: Comparison Type:

Value(s):

9 - 18 Copyright 2008, Oracle. All rights reserved.


Using Declarative Built-in Rules:
List Validator

Ensures that value is in (or not


in) a list defined by:
Literal values
SQL query
VO attribute
View accessor

Although you can define at entity or


attribute level, the List validator
pertains to an entity attribute.

9 - 19 Copyright 2008, Oracle. All rights reserved.


Using Declarative Built-in Rules:
Range Validator

For a range validator, you


specify:
Operator: Between or
Not Between
Range: Minimum and
Maximum values

Although you can define at entity or attribute level, the range


validator pertains to an entity attribute.

9 - 20 Copyright 2008, Oracle. All rights reserved.


Using Declarative Built-in Rules:
Regular Expression Validator

For a regular expression validator, you specify:


Operator: Regular Expression:

Qualifier(s):
Predefined Expressions:

9 - 21 Copyright 2008, Oracle. All rights reserved.


Using Declarative Validation: Built-in Rules

Built-in rules greatly reduce the need for coding.


Include the following types of rules:
Defined only at entity level:
Collection validator
Unique key validator
Can be defined at entity or attribute level, but pertain to an EO
attribute:
Compare validator
Key exists validator
Length validator
List validator
Range validator
Regular expression validator
Can be defined at entity or attribute level to validate the EO or the
attribute:
Script Expression validator
Method validator

9 - 22 Copyright 2008, Oracle. All rights reserved.


Using Declarative Built-in Rules:
Script Expression Validator

To define a Groovy expression, you:


Omit braces {}
Use newValue for new
value of current attribute
Write code to return true
or false and/or call
adf.error.raise or
adf.error.warn
Can use aggregate
functions on RowSet
Can use ternary operator
Click Test to check syntax

9 - 23 Copyright 2008, Oracle. All rights reserved.


Using Declarative Custom Rules:
Entity-Specific Rules (Method Validators)

Method validators:
Extend declarative rules for entities or attributes
Call Java methods in your entity object (not in a separate
class as a global rule is)
Are called automatically during validation cycle
Must:
Be defined as public boolean validateOrder(){
public if( // add your validation code )
Return a Boolean return true;
else
value return false;
Be named like }
validateXXX()

9 - 24 Copyright 2008, Oracle. All rights reserved.


Using Declarative Custom Rules:
Creating an Entity-Specific Method Validator
Create at either the entity or attribute level:

Adds a method to the Add your own code


<EO>Impl.java file to the validateXXX() method

9 - 25 Copyright 2008, Oracle. All rights reserved.


Using Declarative Global Validation Rules

Custom global validation rules are available to assign


declaratively to any business components by using the
following steps:
1. Create a Java class that implements the
JboValidatorInterface interface in the
oracle.jbo.rules package.
2. Modify the code for the validation rule.
3. Assign the rule to a business component object.

9 - 26 Copyright 2008, Oracle. All rights reserved.


Creating the Java Class for a Global Rule

Purpose of rule: Compare any two dates in any entity object


and raise an exception if the first date is after the second date.
Name of Java class

Name that displays


in rule type list

9 - 27 Copyright 2008, Oracle. All rights reserved.


Examining the Generated Skeleton Code

public class CompareDates implements JboValidatorInterface {


private String description = "Returns true if date1 is not after date2";
public CompareDates() {
}
public boolean validateValue(Object value) {
return true;
}
public void validate(JboValidatorContext ctx) { This method is executed
if (!validateValue(ctx.getNewValue())) {
when validation occurs.
throw new
ValidationException("model.CompareDates validation failed");
}
}
public String getDescription() {
return description;
}
public void setDescription(String str) {
description = str;
}
}

9 - 28 Copyright 2008, Oracle. All rights reserved.


Modifying the Code for the Global Rule

public class CompareDates implements JboValidatorInterface {


private String description = "Return true if date1 is not after date2";
private String earlierDateAttrName = "";
1 private String laterDateAttrName = "";

public void validate(JboValidatorContext ctx) {


3 This method is executed
if (validatorAttachedAtEntityLevel(ctx)){
EntityImpl eo = (EntityImpl)ctx.getSource(); when validation occurs.
Date earlierDate = (Date)eo.getAttribute(getEarlierDateAttrName());
Date laterDate = (Date)eo.getAttribute(getLaterDateAttrName());
if (!validateValue(earlierDate,laterDate)) {
throw new ValidationException("model.CompareDates validation failed");
}
}
else {
throw new RuntimeException("Rule must be at entity level");
}
}
private boolean validateValue(Date earlierDate, Date laterDate) {
4 return (earlierDate == null) || (laterDate == null) ||
(earlierDate.compareTo(laterDate) <= 0);
}
private boolean validatorAttachedAtEntityLevel(JboValidatorContext ctx) {
2 return ctx.getOldValue() instanceof EntityImpl;
}

9 - 29 Copyright 2008, Oracle. All rights reserved.


Assigning the Global Rule to an Object

Generate the entity Java class.

Select the
global rule
from
the Rule
Type list.
Create a new validator for an
attribute or for the entity.

Provide
values for
any
properties.

9 - 31 Copyright 2008, Oracle. All rights reserved.


Testing the Global Validation Rule

When you change the date


to violate the validation rule and
attempt to commit the change,
an error message displays
the run-time exception.

9 - 32 Copyright 2008, Oracle. All rights reserved.


Using Programmatic Validation

Create custom methods in the EntityImpl.java file:


public boolean checkOrderMode(){
if ( ("ONLINE".equals(getOrderMode())) &&
(getCustomerEmail() == null))
{
return false;
}
else {
return true;
}}

Call in overridden validateEntity() method:


protected void validateEntity(){
if (!checkOrderMode()) {
throw new JboException("Online order must have email");
}
super.validateEntity();
}

9 - 33 Copyright 2008, Oracle. All rights reserved.


Debugging Custom Validation Code
with the JDeveloper Debugger

JDeveloper Debugger is
useful for pinpointing
problems in your custom
validation code.
Set source breakpoints to
pinpoint problems.
Set exception breakpoints
to stop when a particular exception
is thrown.
At breakpoints you can execute code one line at a time
and view variable values.
To run a file in debug mode, right-click and select Debug.

9 - 34 Copyright 2008, Oracle. All rights reserved.


Using a Domain to Create
Custom-Validated Data Types

Domains are Java classes that extend the basic data types
(String, Number, Date , and so on).
You can create your own data types that encapsulate
specific validation rules.
Use domains for more complex validation:
Format of a phone number
Validity of a URL
Validity of an email address
Checksum of digits of a credit card number
Validation is done by the domain constructor.
A domain is not bound to a particular entity or attribute.

9 - 35 Copyright 2008, Oracle. All rights reserved.


Creating and Using a Domain

1. Create the domain.


2. Add validation code to the validate() method in the
domains Java file.
3. Edit an entity and change the type of an attribute to the
domain.

URLDomain
appears in the
list of types.

9 - 36 Copyright 2008, Oracle. All rights reserved.


Coding Validation in a Domain

The validate() method is called by the domains constructor


(mdata is a private String variable containing the string to be
validated):
Example: URLDomain verifies that an attribute is a valid URL.

protected void validate()


{
try {
// Make sure the user entered a valid URL
java.net.URL u = new java.net.URL(mData);
}
catch (java.net.MalformedURLException e)
{throw new
oracle.jbo.domain.DomainValidationException
("Invalid URL"); }
}

9 - 37 Copyright 2008, Oracle. All rights reserved.


Specifying Validation Order

General validation order:


Changed attributes are validated
when they lose focus.
Changed entities are validated at
commit time.
In compositions, child entities are
validated before parent entities.
You can control order of validation
within attributes or an entity.
You can specify validation at
transaction level for some
validators.
9 - 38 Copyright 2008, Oracle. All rights reserved.
Summary

In this lesson, you should have learned how to:


Describe the types of validation available to ADF BC
applications
Decide which validation options are appropriate for
different validation requirements
Use the declarative validation options provided in
JDeveloper
Create programmatic validation
Use domains in validation

9 - 39 Copyright 2008, Oracle. All rights reserved.


Practice 9 Overview:
Implementing Validation

This practice covers the following topics:


Using declarative validation
Using programmatic validation
Creating and using a domain
Testing the validation

9 - 40 Copyright 2008, Oracle. All rights reserved.

Anda mungkin juga menyukai