Skip to main content

On Validate

Last updated 30/11/2021

On Validate Event

The OnValidate event executes on the client.

Function Signature
OnValidate(sender, context, result) : FiveError;

ParameterTypeDescription
senderFormFieldAn instance of the form field object.
contextMap<string,any>Optional map of string to values.
resultFiveErrorA Five error to set the result and return the error.

Description

Field validation performs accuracy checks on the data entered in to the field using the OnValidate event. After the user has entered the data in the field, if there is any incorrect information Five will instantly notify the user via a message and they will be forced back to the field. When a user interacts with the field, the event will be detected and the code will execute.

The validation of fields occurs on the client-side, however, a server-side validation may be called from the client to check the validity of the input data.

Client-Side Validation

Client-side validation occurs in the browser and does not require a round trip to the server, this allows for quick feedback to the user informing them the required data is incorrect.

Server-Side Validation

You can call another validation from the server-side after verifying from the client-side. The feedback is given to the client after the server-side validation process is completed.

Example

In the Portfolio Application, we can add an On Validate event on the Sells form to ensure the amount of stock being sold is held.

The below function is performing the following:

  • sender.form is used to get the form object.
  • sender.value is used to get the value in the field holding the validation check.
  • form.metadata is used to get the results from the query attached to a lookup field to know the current holding.
  • five.createError is used to return an error if the validation fails.
Validation to check the stock being sold is held.
function CheckStockQuantity(sender: any, context: any, result: FiveError): FiveError {
const form = sender.form
const qty = parseInt(sender.value);
if (qty > form.metadata.StockKey.Holding) {
return five.createError(result, 'Stock quantity exceeds current holding');
}

return five.success(result, '');
}

Prerequisites

  • Query must be written in the Queries view with the syntax to receive the metadata required.
  • Lookup Query must contain a key field and a value field at a minimum.
  • Lookup Query must be attached to a field on the form for the function to use. For this example, The Stock field.
  • Function must be saved in the Functions view.
  • Form must be saved in the Forms view.

Steps to Attach a Function to the On Validate Event

  1. Select Forms in the menu.
  2. Select the required form record in the list.
  3. Click the Pages tab.
  4. Select the required page record.
  5. Click the Fields tab.
  6. Select the required form field record.
  7. Click the Events tab.
  8. Click the Edit button in the form app bar.
  9. Use the lookup icon in the On Validate and select the Function ID.
  10. Click the Save button in the form app bar.
  11. Click both Save buttons in the stacked form app bars.
Attach Function to the On Validate Event
Figure 1 - Attach a function to the On Validate event