Skip to main content

On Validate

Last updated 25/07/2025

This documentation will demonstrate how the On Validate event works. The On Validate event is a client-side event. After a value has been entered into a form field and a user leaves the field, the function attached to the On Validate event will execute and create an error which will display a message if there is an invalid value in the field. The user will be forced back to the field to change the data.



The Orders application is used to demonstrate the On Validate event.

Execute an On Validate Event

The GetProductPrice query gets the ProductKey, Name, Price, and Quantity fields from the Product table. The product names will be listed in the Product field's lookup on the Order Items form. The Quantity query field is provided as metadata to the Order Items form when attached as a _LookupQuery providing how much stock is available for the selected product. This metadata can then be used by a function attached to the form.

The GetProductPrice query also has the Price field supplied as metadata and this is being used for the On List Select event.

MySQL
Selecting the ProductKey, Name, Price, Quantity fields
SELECT
Product.ProductKey AS ProductKey,
Product.Name AS Name,
Product.Price AS Price,
Product.StockQuantity AS Quantity
FROM
Product

The GetProductPrice query is saved in the Queries view in Five.


GetProductPrice query
Figure 1 - GetProductPrice query

info
Five automatically creates query fields with a data type of Text, a size of 50, and a display type of _Text. You can edit this on the Fields page. The image below shows the Quantity field has been edited to have an Integer data type, a size of 4, and an _Integer display type.

Quantity query field
Figure 2 - Quantity query field

The

GetAvailableQuantity()
function uses the
getMetadata()
function on the
Five
object to get the ProductKey form field ID and the Quantity query field ID. The key uniquely identifies the product and the Quantity field is the metadata coming from the GetProductPrice query. The available product quantity will be validated before a user can place their order and if an error is created a message will be displayed to the user and they won't be able to save their order without changing the quantity.

JavaScript
Getting the metadata provided by the GetProductPrice query
function GetAvailableQuantity(five, context, result)  {
const qty = parseInt(five.field.Quantity);
if (qty > five.getMetadata('ProductKey', 'Quantity')) {
return five.createError(result, "Sorry, we don't have that quantity available!");
}

return five.success(result);
}

The

GetAvailableQuantity()
function is saved in the Functions view in Five.


GetAvailableQuantity function
Figure 3 - GetAvailableQuantity function

The GetProductPrice query is attached to the Product field with a display type of _LookupQuery on the Order Items form. The metadata to this field can now be used by the

GetAvailableQuantity()
function.


Product field
Figure 4 - Product field

The

GetAvailableQuantity()
function is attached to the On Validate event for the Quantity field.


On Validate field
Figure 5 - On Validate field

In the Orders application, you can see all the products and the quantity available in the Products view. If we select a customer and drill down to place an order on the Order Items page on the Orders form, when a quantity is entered into the Quantity field that is larger than the amount available in the system an error will be created and the user will receive a message and be forced to change the quantity.

note
You may need to refresh your screen to see the gif.

On Validate event
Figure 6 - On Validate event