# Adding and Editing a Validation Rule

While editing an entity object with the Entity Object Editor or editing entity attributes in the Attribute Editor, you can add and edit validation rules in the Validation page. The rules could be predefined in JDeveloper or be your own custom rules.

#### To add a validation rule:

1. In the Validation page, select an attribute or entity object in the Declared validation rules list and click Add.

2. In the Add Validation Rule dialog, choose a rule from the Rules list.

3. Specify the information needed by that rule.

4. Click OK.

#### To edit a validation rule:

1. In the Validation page, select the validation rule in the Declared validation rules list and click Edit.

2. In the Add Validation Rule dialog, choose a rule from the Rules list.

3. Specify the information needed by that rule.

4. Click OK.

#### To use CompareValidator, ListValidator, or RangeValidator:

If you choose CompareValidator, ListValidator, or RangeValidator, you must also choose an operator and the type of comparison you want to make.

| CompareValidator | ListValidator | RangeValidator
Description | Performs a logical comparison between the attribute and a value. The comparison can be to a literal value, a SQL query, or the results of a view object query. | Compares an attribute against a list of values. The attribute value can be compared against a list of fixed literal values, a list of values returned from a SQL query, or a list of values returned for an attribute of a view object. | Tests for attribute values within specified minimum and maximum values. Note that this validator performs an inclusive range comparison.
Attribute | The name of the attribute to which you are applying the validation rule. | The name of the attribute to which you are applying the validation rule. | The name of the attribute to which you are applying the validation rule.
Operator | Equals NotEquals LessThan GreaterThan LessOrEqualTo GreaterOrEqualTo | In Not In | Between Not Between
Values | Choose one of: Literal Value Type a value in the Enter literal value field. Each entry must be on a new line, separated by Return. Query Result Type a SQL query in the Enter SQL statement field. This statement will be executed at the time of validation; the first column from the select list will define the list of values. View Object Attribute Select a view object in your project from the Select Attribute tree. At runtime, this view object will be executed at the time of validation and the first row returned from the view object will be used to get the attribute value for comparison. | Choose one of: Literal Value Query Result View Object Attribute Each value must be entered on it's own line - do not use commas or spaces to separate values. Lines with no string in them will be ignored. JDeveloper does not evaluate the contents of the list for validity, type, or duplicate values. | Define the range by typing minimum and maximum values. Note that this validator performs an inclusive range comparison.

The Attribute field is available only when you are adding a validation rule: it is not available when you are editing an existing rule. By default, if the rule is being applied on an attribute level (in the Rules panel), and then that attribute will be selected in this list box. However, this does not have to be the same attribute that you chose on the Validation page; any other attribute can be selected for validation. For example, for a rule "validate `DeptNo` whenever an `EmpNo` has changed", the rule is being applied to `EmpNo`, however, in this drop-down list box, `DeptNo` should be selected.

#### To use MethodValidator:

A method validator invokes a custom, or "user-defined", method.

1. In the Add Validation Rule dialog, choose MethodValidator from the Rules list.

   A list of the methods you have defined for the project appears.

2. Select a method name.

3. Click OK.

#### To use a custom validator:

Typically, you follow these steps.

1. In the Add Validation Rule dialog, choose the custom validator from the Rules list.

   The editable field contains a table of properties and their corresponding values.

2. Type the values that you want to validate the attribute against.

3. Click OK.

If there are custom methods defined in the Java class for this entity object, for the selected validation level, those methods will be displayed in this list. Candidate methods should be `public`, return `boolean` and start with the keyword `validate`. Also, for an attribute, the method should have one argument of the type of the attribute.

For example, for an entity-level validation, the candidate methods could be:

> `public boolean validateDepartment()`
> `public boolean validateDepartmentsForProperLocations()`

For an attribute `DeptNo` of type `Number`, candidate methods could be:

> `public boolean validateDeptNo(Number value)`
> `public boolean validateDepartmentNumberForTwoDigits()`

These custom methods should be added to the Java file before the Entity Object Wizard is started or they won't appear in the Available Methods list.

A custom validator typically consists of a list of properties and values against which an attribute is compared. The contents of the Validation Rule panel changes depending on your choice of rule. These rules are Java Beans and, thus, could have their own customizers, per the Java Beans specifications. These customizers will be displayed in this panel allowing you to edit the properties of the Bean. If there are no customizers defined for a Bean, this panel will display a Property-Value editor where Properties are listed on the left column and their current values listed on the right column. The values can be modified using this panel. (For an example of a customizer, see `oracle.jbo.server.rules.CompareValidatorCustomizer.java` which is a custom editor for `oracle.jbo.server.rules.JboCompareValidator.java`.)