How to Use the “Validate” Data Store Event
Copy
The following sample code demonstrates how to prevent a user from updating a property in the data store. Validate event methods have the following parameters available:
- itemId – the GUID of the current context item
- name – the name of the property being updated
- value – the property value being validated. For item properties, this is an object with the selected item’s id, type, and keyedName.
Example 1
Use Case: We don’t want users to update prop_a anywhere in the client – on forms, relationship grids, or search grids.
Solution: Create the following method and configure it as a Validate event on prop_a of an ItemType.
...
if (name === ‘prop_a’) {
return {
valid: false,
validationMessage: `${name} property can’t be changed`
};
}
...
Example 2
Use Case: The owner and manager of an item cannot be the same identity.
Solution: Create the following method and configure it as a Validate event on both the owned_by_id and managed_by_id properties of an ItemType.
...
const currentItem = aras.itemsCache.getItem(itemId) || aras.itemsCache.getItemByXPath(`//Item[@id="${itemId}"]`);
const owner = aras.getItemProperty(currentItem, ‘owned_by_id’, '');
const manager = aras.getItemProperty(currentItem, ‘managed_by_id’, '');
// one or both of the fields may be null
if (value === null) {
return {
valid: true
};
}
if (owner === value.id || manager === value.id) {
return {
valid: false,
validationMessage: `Cannot update ${name}. Owner and manager cannot be the same.`
};
}
return {
valid: true
};
...