The latest version of the library, compatible with the new application app.foxentry.com.
The Foxentry JavaScript API v2 allows you to control the validation of mapped inputs directly from JavaScript. The library is designed for the new Foxentry application and works with inputs that are configured and mapped within your project.
To use the JavaScript API v2, you must have a Foxentry project created in the app.foxentry.com application and have the integration JavaScript library embedded on your website.
The onFoxentryProjectLoad global function is also available in version v2. If you declare it in your code, the library will automatically call it after it loads.
function onFoxentryProjectLoad() {
console.warn("Foxentry v2 is loaded");
}The isReady() method indicates whether the library is fully loaded and ready for use. We recommend using this method before running validation or reading validation statuses.
var ready = Foxentry.isReady();
console.warn(ready);true or false
The validate() method performs validation on all mapped inputs within the specified parent. If no parent is provided, validation is performed on all mapped inputs.
Parameters
parent- optional parent element or selector of typeHtmlElementOrSelector
await Foxentry.validate();var form = document.querySelector("#orderForm");
await Foxentry.validate(form);Foxentry.validate("#orderForm").then(function () {
console.warn("Validation complete");
});The getValidationStatus() method returns the current validation status of the mapped inputs. If the parent parameter is provided, it returns only the status of the inputs within that parent. If the parameter is not provided, it returns the status of all mapped inputs.
Parameters
parent- optional parent element or selector of typeHtmlElementOrSelector
var status = Foxentry.getValidationStatus();
console.warn(status);var status = Foxentry.getValidationStatus("#orderForm");
console.warn(status.isValid);
console.warn(status.inputs);export interface InputValidationInfo {
isValid: boolean;
isLoading: boolean;
mark: 'valid' | 'invalid' | 'alert' | null;
message: string | null;
ref: HTMLElement;
}
export interface InputsValidationStatus {
inputs: InputValidationInfo[];
isValid: boolean;
isLoading: boolean;
}inputs- a list of all mapped inputs within the given scope and their current validation statusesisValid- indicates whether all included inputs are currently validisLoading- indicates whether validation is currently in progress for any of the included inputs
isValid- indicates whether a specific input is currently evaluated as validisLoading- indicates whether validation is currently in progress for a specific inputmark- the input's state; can take the valuesvalid,invalid,alert, ornullif no flag is setmessage- the validation message for the given input; returnsnullif not availableref- a reference to the specific DOM element of the input
{
"inputs": [
{
"isValid": true,
"isLoading": false,
"mark": "valid",
"message": null,
"ref": "HTMLElement"
},
{
"isValid": false,
"isLoading": false,
"mark": "invalid",
"message": "The entered value is invalid",
"ref": "HTMLElement"
}
],
"isValid": false,
"isLoading": false
}The validate() and getValidationStatus() methods can accept an optional parent parameter, which limits processing to only the mapped inputs inside the specified parent.
type HtmlElementOrSelector = HTMLElement | JQuery | string | nullYou can pass:
HTMLElement- a specific DOM element, such as a form or containerJQuery- a jQuery object containing the parent elementstring- a CSS selector used to locate the parent elementnull- an empty value; effectively the same as not passing the parameter
If you do not pass the parent parameter, all mapped inputs on the page will be processed.
var formElement = document.querySelector("#orderForm");
var jqueryForm = $("#orderForm");
var selector = "#orderForm";
var emptyParent = null;The setCallbacks() method is used to set callback functions for individual validator types. The callback is invoked after the validation of the given validator has completed and allows you to further process the validation result in JavaScript.
You can set callbacks directly or within onFoxentryProjectLoad(), which the library automatically calls in v2 after loading.
export type FoxentryCallback = (input: HTMLElement, validation: ValidationCallbackValidationArgument) => unknown;
export type FoxentryCallbacksType = {
[validatorType in ValidatorTypes]: FoxentryCallback;
};
export interface ValidationCallbackValidationArgument {
group: GroupValidationInfo;
}
export interface GroupValidationInfo {
isValid: boolean;
isLoading: boolean;
validatorType: ValidatorTypes;
inputs: Array<InputValidationInfo>;
}The first argument of the input callback contains a reference to the input field that was validated. The second argument, validation, contains an object of type ValidationCallbackValidationArgument, in which the validation result is available in the group attribute.
group- details of the validation group to which the currently validated input belongs
isValid- indicates whether the group of inputs is currently evaluated as validisLoading- indicates whether validation is currently in progress for the group of inputsvalidatorType- the type of validator to which the callback belongsinputs- a list of inputs included in the given validation group, including their current status
The library supports the following validator types:
emailphonecompanynamelocation
function onFoxentryProjectLoad() {
function emailCallback(input, validation) {
console.warn("Email input:", input);
console.warn("Validation group result:", validation.group);
}
function phoneCallback(input, validation) {
console.warn("Phone input:", input);
console.warn("Validator type:", validation.group.validatorType);
console.warn("Validated inputs:", validation.group.inputs);
}
Foxentry.setCallbacks({
email: emailCallback,
phone: phoneCallback,
company: function (input, validation) {
console.warn("Company:", input, validation);
},
name: function (input, validation) {
console.warn("Name:", input, validation);
},
location: function (input, validation) {
console.warn("Location:", input, validation);
}
});
}This section describes the most important differences when migrating from v1 to v2. The biggest changes concern working with callbacks.
In v1, callbacks were typically set using FoxentryBuilder.setCallbacks(...) inside onFoxentryProjectLoad().
In v2, onFoxentryProjectLoad() remains available, but callbacks must be registered using Foxentry.setCallbacks(...).
function onFoxentryProjectLoad() {
FoxentryBuilder.setCallbacks({
address: function (data, validatorInfo) {
console.warn(data);
console.warn(validatorInfo);
}
});
}function onFoxentryProjectLoad() {
Foxentry.setCallbacks({
location: function (input, validation) {
console.warn(input);
console.warn(validation.group);
},
company: function (input, validation) {},
email: function (input, validation) {},
name: function (input, validation) {},
phone: function (input, validation) {}
});
}In v1, the callback also accepted two arguments (data, validatorInfo).
In v2, the callback still uses two arguments, but both argument formats are new:
(input: HTMLElement, validation: ValidationCallbackValidationArgument) => unknowninput- the first argument is now the validated DOM element (HTMLElement) (in v1 the first argument wasdata)validation.group- the second argument is now wrapped inValidationCallbackValidationArgument(in v1 it wasvalidatorInfo)
If you use a callback for address in v1, you need to use the location key in v2.
FoxentryBuilder.setCallbacks({
address: function (data, validatorInfo) {
console.warn(data);
console.warn(validatorInfo);
}
});Foxentry.setCallbacks({
location: function (input, validation) {
console.warn(validation.group.inputs);
},
company: function () {},
email: function () {},
name: function () {},
phone: function () {}
});- You can keep the
onFoxentryProjectLoad()function; the library will also call it in v2 after loading. - Inside
onFoxentryProjectLoad(), replaceFoxentryBuilder.setCallbacks(...)withFoxentry.setCallbacks(...). - Update data handling in callback code: the first argument is now a DOM element and the second argument exposes results via
validation.group. - Rename the
addresscallback key tolocation. - After making the changes, verify the behavior by calling
await Foxentry.validate(...)and checking the result viaFoxentry.getValidationStatus(...).