Setting Invalid Fields for the UI in an Object-Level Validation Rule
When a field-level validation rule that you've written returns false, ADF signals the failed validation with an error and the field is highlighted in the user interface to call the problem to the user's attention.
However, since object-level validation rules involve multiple fields, the framework does not
know which field to highlight in the user interface as having the problematic value. If you
want your object-level validation rule to highlight one or more fields as being in need of
user review to resolve the validation error, you need to assist the framework in this process.
You do this by adding a call to the adf.error.addAttribute()
function in your
validation rule script before returning false
to signal the failure.
For example, consider the following rule to enforce: A contact cannot be his/her own manager.
Since the Id
field of the Contact
object cannot be changed,
it will make sense to flag the Manager_Id
field — a secondary field related
to the Manager
lookup field — as the field in error to highlight in the user
interface. Here is the example validation rule.
-
Rule Name:
Contact_Cannot_Be_Own_Manager
-
Error Message:
A contact cannot be his/her own manager
Rule Body
// Rule depends on two fields, so must be
// written as object-level rule
if (Manager_Id_c == Id) {
// Signal to highlight the Manager field on the UI
// as being in error. Note that Manager_Id field
// is not shown in the user interface!
adf.error.addAttribute('Manager_c')
return false
}
return true